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

422 Responses so far

«Oldest   ‹Older   401 – 422 of 422   Newer›   Newest»
  1. karthik says:

    Great the given information and after refer that post,i get new energy because of all given information very excellent and real words to implemented for you.Thank you so much shared that valuable information. PCB Design Training Center in Chennai ! velachery|

  2. thank you for such a great article with us. hope it will be much useful for us. please keep on updating.. Advanced JAVA Training Center in Chennai |

  3. Neil says:

    Very nice blogs!!! I have to learning for lot of information for this sites...Sharing for wonderful information. Thanks for sharing this valuable information to our vision. You have posted a trust worthy blog keep sharing.… ISTQB Certification in Chennai |

  4. karthik says:

    Very happy to see this blog. Gives a wonderful information with coded explanaion. Thank you for this blog. very useful to me. Prince2 Certification Center in Chennai |

  5. karthik says:

    Well Said, you have furnished the right information that will be useful to anyone at all time. Thanks for sharing your Ideas. Selenium Automation Training center in chennai |

  6. vijay says:

    wonderful article. Very interesting to read this article.I would like to thank you for the efforts you had made for writing this awesome article.
    Good information. Thanks for sharing with us..
    Salesforce Training in Chennai

    Salesforce Online Training in Chennai

    Salesforce Training in Bangalore

    Salesforce Training in Hyderabad

    Salesforce training in ameerpet

    Salesforce Training in Pune

    Salesforce Online Training

    Salesforce Training

  7. Natasha says:

    Amazing write up. You have delivered useful information. Thanks for posting and keep blogging. PCB Design Training Center in Chenna |

  8. Neil says:

    This was a worthy blog. I enjoyed reading this blog and got an idea about it. Keep sharing more like this. MS Office Training Institute in Chennai|

  9. karthik says:

    Brilliant article. The information I have been searching precisely. It helped me a lot, thanks. Keep coming with more such informative article. Would love to follow them. Graphic design Training Institute in Chennai|

  10. Natasha says:

    Thanks for sharing this valuable information to our vision. You have posted a trust worthy blog keep sharing.Regards, Data Science Training Institute in Chennai |

  11. Neil says:

    In the beginning, I would like to thank you much about this great post.I like your writing style and I hope you will keep doing this good working AWS Exam Center in Chennai |

  12. Wow...Excellent informative blog, really helpful. Thank you. Final Year Projects For Phd in Chennai |

  13. jeffery says:

    Excellant post!!!. The strategy you have posted on this technology helped me to get into the next level and had lot of information in it. Final year projects For CS in Chennai |

  14. Very interesting and good article, it is very useful to me... keep rocks.. thanks for sharing your valuable time and views.... Final year projects For Embedded Systems in Chennai |

  15. Usadev says:

    this blog is really useful and it is very interesting thanks for sharing , it is really good and exclusive. PMP Certification Center in Chennai |

  16. advikshan says:

    Really an awesome post. I wondered by reading this blog post. Thanks a lot for posting this unique post which you have shared with us. Keep on posting like this exclusive post with us.Python Training institute in Chennai |

  17. Neil says:

    I really enjoy simply reading all of your weblogs. Simply wanted to inform you that you have people like me who appreciate your work. Hardware and Networking Training institute in Chennai |

  18. Neil says:

    I really enjoy simply reading all of your weblogs. Simply wanted to inform you that you have people like me who appreciate your work. Hardware and Networking Training institute in Chennai |

  19. There was very wonderful information and that's great one. I really appreciate the kind words, thanks for sharing that valuable information. illustrator Training institute in Chennai |

  20. Nice Post! It is really interesting to read from the beginning & I would like to share your blog to my circles, keep your blog as updated. JAVA Training Institute In Chennai |

  21. karthik says:

    Your blog is awesome..You have clearly explained about it ...Its very useful for me to know about new things..Keep on blogging.. Final year projects For CSE in Chennai |

«Oldest ‹Older   401 – 422 of 422   Newer› Newest»

Leave a Reply