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__':

30 Responses so far

  1. Usually I do not read post on blogs, but I would like to say that this write-up very forced me to try and do it! Your writing style has been surprised me. Great work admin.Keep update more blog.
    Python Training in Chennai

  2. It is amazing and wonderful to visit your site.Thanks for sharing this information,this is useful to me...
    Android Training in Chennai
    Ios Training in Chennai

  3. Very impressive and informative article.. thanks for sharing your valuable information.. it is very useful and easy to learn as well... keep rocking and updating... looking further..
    Austere Technologies |Internet Of Things

  4. revathi says:

    The great service in this blog and the nice technology is visible in this blog. I am really very happy for the nice approach is visible in this blog and thank you very much for using the nice technology in this blog...
    Good Technical Boot Camp in Chennai | Summer Course in Porur

  5. rajendran says:

    Good and awesome blog post, thanks for sharing your information.. it is very useful to me.. keep rocks and updating..
    Best Summer Courses for Hardware Networking in Chennai | No.1 Summer Course in Guindy

  6. Ruban k says:

    Well Said, you have furnished the right information that will be useful to anyone at all time. Thanks for sharing your Ideas.
    Best Summer Courses for Business Administration in Chennai | No.1 Vacation Classes in Velachery

  7. massil says:

    wow...nice blog, very helpful information. Thanks for sharing.

    Best Software Company in USA | Austere Technology Solutions

  8. Nice blog with excellent information. Thank you, keep sharing.

    Best Software Security Services | Austere Technology Solutions

  9. dev says:

    Great article, really very helpful content you made. Thank you, keep sharing.

    Best Cloud Services | Austere Technology Solutions

  10. AUSTERO says:

    Very good informative article. Thanks for sharing such nice article, keep on up dating such good articles.

    Best Quality Management Services | Austere Technology Solutions

  11. Looking very great article consists of very valuable information, thanks much for posting here keep updating us...
    Best Online Software Training Institute | Python Training

  12. deepti says:

    This article is very informative and nice. It is easy to understand to us.
    Good AWS Training in Tambaram | AWS Courses in Adyar | AWS Institute in Besant Nagar

  13. ancy says:

    Hi i am newbie to your blog. I have read your post. It having very interesting post and i am never getting tired.keep sharing....
    AWS Certification Exam Centers in Taramani | AWS Training in Shozhinganallur

  14. this list is enough to get my keyword in the first page.. thank you very very much..
    Python Certification Centers in Chennai | Best Python Exam in Perungudi

  15. Was looking out for a list like this.This surely helps.You have also sorted out with page rank which is commendable.Thanks for sharing.
    Java Project Center in Chennai | No.1 Java Project Training in Porur

  16. This is the best Blog which i recently read and Thanks for sharing the Information.. You can also see our training webpage..
    python training in hyderabad

  17. Avinash says:

    Very informative blog, really helpful. Thank you.

    cs course eligibility | ISFS

  18. Avinash says:

    Wow...Excellent informative blog, really helpful. Thank you.

    Best CMA Training in hyd | ISFS

  19. Thank you for sharing your article and this site is very informative and also very useful for us. And not only that the content of your blog is awesome. Keep going on and sharing lot of interesting information with this article.see more: Python Online Training

  20. Avinash says:

    Your article gives lots of information to me. Thanks for sharing.
    acca course in hyderabad | ISFS

  21. Ruban k says:

    Very interesting blog which helps me to get the in depth knowledge about the technology, Thanks for sharing such a nice blog...
    JAVA Project Center in Chennai | JAVA Project in Thiruvanmiyur

  22. zara says:

    This blogs seems to be interesting for Python Training in Hyderabad

Leave a Reply