Thursday, April 12, 2012

Python Script for Remote Server Connection through SSH2

305 comments
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 -
wget http://pypi.python.org/packages/source/p/paramiko/paramiko-1.7.7.1.zip
unzip  paramiko-1.7.7.1.zip
cd paramiko-1.7.7.1
python setup.py build
python setup.py install
Now check whether the installation was successful or not -
python
>>> 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.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    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)
  backup_dirs.sort()
  #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):
  ssh.close()

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..."
  close_ssh_connection(ssh)
  print "Done"
  print ""
  return

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

Install Mysql-Python

3 comments
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 - pypi.python.org/pypi/MySQL-python.

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 -  http://pypi.python.org/pypi/setuptools

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

wget http://pypi.python.org/packages/2.7/s/setuptools  \
/setuptools-0.6c11py2.7.egg#md5=fe1f997bc722265116870bc7919059ea
sh setuptools-0.6c11-py2.7.egg

Install MySQL-python:

wget http://pypi.python.org/packages/source/M/MySQL-python/MySQL-python-1.2.3.tar.gz
tar -zxvf  MySQL-python-1.2.3.tar.gz
cd  MySQL-python-1.2.3
python setup.py build 
python setup.py 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:

vi mysql_test.py

#!/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
    return
   
if __name__ == '__main__':
 main()



Execute Script:

python mysql_test.py
Database version : 5.1.48-community-log 


For more basics of Mysql programming with Python, you may visit
http://zetcode.com/databases/mysqlpythontutorial
Continue reading →

The beginning of Python

0 comments
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
http://code.google.com/edu/languages/google-python-class.

Another very good python tutorial site for the beginners is http://www.learnpython.org/.
I worked with these sites and had some basic skills. I installed Python 2.7 in my machine and developed my first script hello.py.

hello.py
#!/usr/bin/python2.7 -tt

def main():
 print "Started learning python"

#boilerplate function
if __name__ == '__main__':
 main()

Executed the scipt

python hello.py

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 →

Labels