Thursday, April 12, 2012

Python Script for Remote Server Connection through SSH2

To make an ssh connection to remote server and scp files from that server through Python script, I used the python package "paramiko". This is a library for making SSH2 connections (client or server). Emphasis is on using SSH2 as an alternative to SSL for making secure connections between python scripts. All major ciphers and hash methods are supported. SFTP client and server mode are both supported too.
 I installed paramiko in my python environment using the following steps -
cd paramiko-
python build
python install
Now check whether the installation was successful or not -
>>> import paramiko

I added the script below where I created functions to open ssh connection, find required directory in remote machine and close that ssh connection.
#!/usr/bin/python -tt
import sys
import commands
import paramiko
import os
import re
import shutil

def open_ssh_connection():
    ssh = paramiko.SSHClient()
    ssh.connect(hostname=source_server, username=source_os_user, password=source_os_password)
    return ssh

def find_backup_dir(ssh):   
  #Change directory to remote server and list the directories
  stdin, stdout, stderr = ssh.exec_command('cd /BACKUP; ls')
  cmd_out = stdout.readlines()
  #Make tuple to string
  cmd_out= ','.join(cmd_out)
  #Find backup directories like backup_020612, here backup_dirs is a list
  backup_dirs = re.findall(r'backup_\w+', cmd_out)
  #Select the last backup directory
  backup_dir = backup_dirs[len(backup_dirs)-1]
  backup_dir=source_dbbackupdir + '/' + backup_dir
  return backup_dir

def close_ssh_connection(ssh):

def main():
  print ""
  print "Opening connection to source server ..."
  ssh = open_ssh_connection()
  print "Done"
  print ""
  print "Searching for latest backup directory in source server ..."
  source_dbbackupdir = find_backup_dir(ssh)
  print "Lastest backup directory =", source_dbbackupdir
  print "Done"
  print ""
  print "Closing connection with the source server..."
  print "Done"
  print ""

if __name__ == '__main__':
Continue reading →
Thursday, April 5, 2012

Install Mysql-Python

In order to connect to Mysql from Python, I used MySQLdb library. It is an interface to the MySQL database server for Python.  For more information -

I installed MySQL-python in my environment following the steps below. 

Import  setuptools:

setuptools is a python package that is used to download, build, install, upgrade, and uninstall Python packages -- easily!  For more information -

And as setuptools was not installed in my machine, I had to make it at first. 

wget  \
sh setuptools-0.6c11-py2.7.egg

Install MySQL-python:

tar -zxvf  MySQL-python-1.2.3.tar.gz
cd  MySQL-python-1.2.3
python build 
python install

Check Installation:

# python
Python 2.7.1 (r271:86832, Jun  8 2011, 12:30:16)
[GCC 4.1.2 20071124 (Red Hat 4.1.2-42)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>import MySQLdb

First Script:


#!/usr/bin/python -tt

import MySQLdb as mdb
import sys
con = None
def main():

    con = mdb.connect(host='localhost',  user='testuser',passwd=t'test_passwd',db='testdb',port=3306)
    cur = con.cursor()
    cur.execute("SELECT VERSION()")
    data = cur.fetchone()
    print "Database version : %s " % data
if __name__ == '__main__':

Execute Script:

Database version : 5.1.48-community-log 

For more basics of Mysql programming with Python, you may visit
Continue reading →

The beginning of Python

For last few days, I was thinking of learning something new for development works. From several options I chose to learn Python. In fact, I was encouraged by google's tutorial for python by Nick Parlante. It really helped me to start python programming quickly. You may get it at -

I also got support materials at

Another very good python tutorial site for the beginners is
I worked with these sites and had some basic skills. I installed Python 2.7 in my machine and developed my first script
#!/usr/bin/python2.7 -tt

def main():
 print "Started learning python"

#boilerplate function
if __name__ == '__main__':

Executed the scipt


I got output -
Started learning python

I was happy and then went on my study and completed my first script to import wordpress blogs to a staging machine from production backup. I'll upload my script later.
However, in the script I had to make SSH connection with different servers, scp file from remote server to local server, connect with mysql and execute queries, restore mysql dump file to new mysql instance. All these tasks were pretty much easy with Python. Lots of library available, I utilized them and made my work.
Continue reading →