psycopyg2 – Get dictionary like query results

Wondering how life would be much easier if you could get the query resultset to be a dictionary and you could get the value by using a keyword on the dictionary. psycopg2 by default returns resultset as a tuple so you have to use row[0], etc to get the values. In order to use row[‘column_name’] from the result, you’ll have to use the extras module provided in the psycopg2. Initiate the cursor accordingly and you’ll be able to get the results in form of dictionary.

#!/usr/bin/python
import psycopg2
import psycopg2.extras

DB_CONNECTION_STRING = "host=%s dbname=%s user=%s password=%s" % (DB_HOST, DB_DATABASE, DB_USER, DB_PASSWORD)
dbConn = psycopg2.connect(DB_CONNECTION_STRING)
cur = dbConn.cursor(cursor_factory = psycopg2.extras.DictCursor)
qSelect = "SELECT server, data, report_date FROM kw_server_raw WHERE processed = FALSE"
cur.execute(qSelect)
results = cur.fetchall()
for row in results:
	print row['server']

SSH/SFTP to remove server to execute scripts without being redirected to terminal

If you use a bash script to sftp/ssh into a remote terminal, you’ll notice the sftp prompt show up on the screen and the scripts beyond sftp will not be executed. You can enclose the remote server scripts in an EOM statement like this in order to do what you intend to.

#!/bin/bash
#-- Go to the bottle server directory and get the existing files into the tars directory
#---------------------------------------------------------------------------------------
cd /home/server/bottle/
tar -cvf /home/varun.verma/backups/provisioning-tars/bottle_server.tar *
cd /home/varun.verma/backups/provisioning-tars/
sftpgs <<EOM
cd /home/admin/dev_tar
put bottle_server.tar
EOM

sshgs <<EOM
cd /home/admin/dev_tar/
tar -xvf bottle_server.tar
rm bottle_server.tar
exit
EOM

Installing Ubuntu (UI Based) on AWS

It took quite a while to figure this out. Eventually – the issue I was having was that I tried to install the standard AWS Ubuntu image. Search for community images with Ubuntu Desktop. After launching this instance, setup a user with sudo access (if you prefer to grant sudo) and install xrdp on the system (apt-get install xrdp). That’s it. Use the standard RDP client to login to the Ubuntu machine like you’ve seen before.

Enable RDP on AWS

Quite a few steps are required to turn on RDP on Ubuntu instance installed on AWS. Here are the steps:

Step 1: Install Updates
# sudo apt-get update
# sudo apt-get upgrade

Step 2: Install XRDP Packages
# sudo apt-get install xrdp

Step 3: Restart the Machine
# sudo shutdown -r now

Step 4: Install xfce 4 desktop
# sudo add-apt-repository ppa:xubuntu-dev/xfce-4.10
# sudo apt-get update
# sudo apt-get install xfce4

Step 5: Configure xfce
# echo xfce4-session >~/.xsession
# sudo service xrdp restart

Step 6: Create a User account
# sudo adduser UserName

Step 7: Add Security Group rule to enable RDP on Incoming
Ports Protocol Source launch-wizard-2
3389 tcp 0.0.0.0/0 ✔

Step 8: Connect to remote machine.
On the welcome screen, click “Use default config”

Here’s the link to the original article: http://www.sysads.co.uk/2014/02/connect-ubuntu-server-13-10-amazon-ec2-xrdp/