Thursday, April 12, 2012

Python Script for Remote Server Connection through SSH2

100 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()

100 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

  23. Hi i am newbie to your blog. I have read your post.Best vacation classes traning for Students

  24. Good and awesome blog post, thanks for sharing your information.. it is very useful to me..Best summer course for school students in chennai training in kanchipuram

  25. karthik says:

    the blog is very useful, interesting and informative. thank you for sharing the blog with us. keep on updating. Best web designing and development courses in kanchipuram|

  26. the blog is very useful, interesting and informative. thank you for sharing the blog with us. keep on updating. Best Tally ERP 9 Training institute in kanchipuram|

  27. kokilaswe 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

    Best AWS Training Academy in Kanchipuram

  28. thenujo says:

    Really very great information to be provided and the All points discussed were worth reading and i’ll surely work with them all one by one.
    Best C and C++ Programming Training Academy in Kanchipuram

  29. I think this is the best article today. Thanks for taking your own time to discuss this topic, I feel happy about that curiosity has increased to learn more about this topic. Keep sharing your information regularly for my future reference. Best RHCE Training Institute in kanchipuram|

  30. Thanks for taking your own time to discuss this topic, I feel happy about that curiosity has increased to learn more about this topic. Keep sharing your information regularly for my future reference. Best tally Erp 9 Training Institute in kanchipuram|

  31. I believe there are many more pleasurable opportunities ahead for individuals that looked at your site. Best Graphic Designing Training Institute in kanchipuram

  32. The best thing is that your blog really informative thanks for your great information.
    Best Graphic Designing Training Institute in kanchipuram|

  33. I think this is the best article today. Thanks for taking your own time to discuss this topic, I feel happy about that curiosity has increased to learn more about this topic. Keep sharing your information regularly for my future reference. Best Web Designing and Development Training Institute in kanchipuram|

  34. Really it was an awesome blog...... Very interesting to read, .It’s very helpful for me, Big thanks for the useful info and keep updating…
    Best Tally Training Academy in Kanchipuram

  35. Thanks for sharing this niche useful informative post to our knowledge...
    Best java Training Academy in Kanchipuram

  36. karthik says:

    Awesome post. Really you are shared very informative concept... Thank you for sharing. Keep on updating...
    Best ccnp Training Academy in Kanchipuram

  37. It’s interesting that many of the bloggers to helped clarify a few things for me as
    Well as giving. Most of ideas can be nice content.
    Best Graphic Designing Training Institute in in Kanchipuram

  38. I found some useful information in your blog, it was awesome to read, thanks for sharing this great content to my vision, keep sharing..
    Best Graphic Designing Training Institute in in Kanchipuram

  39. Your Blog is really awesome with useful and helpful content for us.Thanks for sharing ..keep updating more information.
    Best java Training Institute in in Kanchipuram

  40. Nice post... Really you are done a wonderful job. Thanks for sharing such wonderful information with us. Please keep on updating...
    Best Graphic designing Training Institute in in Kanchipuram

  41. mohanraj says:

    This information is very useful and innovative..I gained more knowledge from this.Thanks for the nice information its very useful to read your blog...
    Best web designing Training Institute in in Kanchipuram

  42. sandhiya says:

    Wonderful article.It is to define the concepts very well.Clearly explain the information.It has more valuable information for encourage me to achieve my career goal.
    Best dally erp 9 Training Institute in in Kanchipuram

  43. Wow, really I am much interested to know our blog content is really good.Great information. Thank you for Sharing.
    Best C++ Training Institute in Kanchipuram

  44. karthik says:

    Awesome Post! I like writing style, the way you described the topics throughout the post. I hope many web reader will keep reading your post at the end, Thanks for sharing your view.
    Best web design Training Institute in Kanchipuram

  45. mani says:

    Your article was so impressive and informative. Its very interesting to read. Thanks for sharing,
    Best web design Training Institute in Kanchipuram

  46. karthik says:

    I believe there are many more pleasurable opportunities ahead for individuals that looked at your site
    Best dotnet Training Institute in Kanchipuram

  47. mani 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
    Best graphic designing Training Institute in Kanchipuram

  48. mohanraj says:

    Thanks for sharing the Amazing Post. It shows your in-depth knowledge on the subject. Keep updating.
    Best ccnp Training Institute in Kanchipuram

  49. mani says:

    Wonderful article.It is to define the concepts very well.Clearly explain the information.It has more valuable information for encourage me to achieve my career goal.
    Best CCNP Training Institute in Kanchipuram

  50. karthik says:

    thank you for such a great article with us. hope it will be much useful for us. please keep on updating..
    Best software testing Training Institute in Kanchipuram

  51. Thanks for sharing such a great information..Its really nice and informative.Great article, thanks for posting...
    Best software testing Training Institute in Kanchipuram

  52. mohanraj 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
    Best Web Designing and Development Training Institute in Kanchipuram

  53. mohanraj says:

    wonderful information. Thanks for sharing this valuable information to our vision. You have posted a trust worthy blog keep sharing
    Best C++ Training Institute in Kanchipuram

  54. Excellent post. I have read your blog it's very interesting and informative. Keep sharing.
    Best Graphic Designing Training Institute in Kanchipuram

  55. mani says:

    This is really too useful and have more ideas from yours. keep sharing many things and thanks for sharing the information.
    Best web Designing Training Institute in Kanchipuram

  56. karthik says:

    Nice blog. I feel really happy to have seen your webpage and look forward to so many more entertaining times reading here. Thanks once more for all the details.
    Best java Training Institute in Kanchipuram

  57. mohanraj says:

    Thank you for taking the time to provide us with your valuable information. We strive to provide our candidates with excellent care and we take your comments to heart.As always, we appreciate your confidence and trust in us.
    Best dally erp 9 Training Institute in Kanchipuram

  58. Aditi Ray says:

    TreasureBox is operated by a group of young, passionate, and ambitious people that are working diligently towards the same goal - make your every dollar count, as we believe you deserve something better.
    Check out the best
    sofa bed
    shoe rack nz

  59. mohanraj says:

    Great blog.you put Good stuff.All the topics were explained briefly.so quickly understand for me.I am waiting for your next fantastic blog.Thanks for sharing.
    Best UIPpath certification Training Institute in Kanchipuram

  60. yamuna says:

    Its really an Excellent post. I just stumbled upon your blog and wanted to say that I have really enjoyed reading your blog. Thanks for sharing…..No:1 Python Programming Training Institute in Kanchipuram

  61. mani says:

    Wonderful article.It is to define the concepts very well.Clearly explain the information.It has more valuable information for encourage me to achieve my career goal.
    Best Web Designing and Development Training Training Institute in Kanchipuram

  62. 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…..Best AWS Training Institute in Kanchipuram

  63. karthik says:

    I have been searching for quite some time for information on this topic and no doubt your website saved my time and I got my desired information. Your post has been very helpful. Thanks.
    Best dally erp 9 Training Training Institute in Kanchipuram

  64. kokilaswe says:

    It’s interesting that many of the bloggers to helped clarify a few things for me asWell as giving. Most of ideas can be nice content.…..Best Software Testing Training Institute in Kanchipuram

  65. Amazing article. Your blog helped me to improve myself in many ways thanks for sharing this kind of wonderful informative blogs in live. I have bookmarked more article from this website. Such a nice blog you are providing !
    Best Graphic Designing Training Training Institute in Kanchipuram

  66. Wonderful blog… You provided very interesting information here. I feel happy to read this post. I hope that you will write many posts like this… Thanks for sharing and Keep updating….. .…..Best Web Designing Training Institute in Kanchipuram

  67. mohanraj says:

    Very interesting, good job and thanks for sharing such blog. It is very interesting to read.
    Its pretty good and well noted. All yourv points are veryuseful.
    Best Python Training Training Institute in Kanchipuram

  68. Wow it is really wonderful and awesome thus it is very much useful for me to understand many concepts and helped me a lot. it is really explainable very well and i got more information from your blog.and once again thanks for sharing.….. .…..Best Graphic Designing Training Institute in Kanchipuram

  69. mani says:

    Excellent post!!!. The strategy you have posted on this technology helped me to get into the next level and had lot of information in it.
    Best web Designing Training Training Institute in Kanchipuram

  70. Thanks for taking your own time to discuss this topic, I feel happy about that curiosity has increased to learn more about this topic. Keep sharing your information regularly for my future reference.….. .…..Best LINUX Training Institute in Kanchipuram

  71. ramya raj says:

    Awesome Post! I like writing style, the way you described the topics throughout the post. I hope many web reader will keep reading your post at the end, Thanks for sharing your view. .…..No:1 AZURE Training Institute in Kanchipuram

  72. karthik says:

    Wonderful!! this is really one of the most beneficial blogs I’ve ever browsed on this subject. I am very glad to read such a great blog and thank you for sharing this good info with us.Keep posting stuff like this.
    Best dally erp 9 Training Training Institute in Kanchipuram

  73. I feel really happy to have seen your webpage and look forward to so many more entertaining times reading here. Thanks once more for all the details.
    Best C++ Training Training Institute in Kanchipuram

  74. Its really an Excellent post. I just stumbled upon your blog and wanted to say that I have really enjoyed reading your blog. Thanks for sharing…..…..No:1 JAVA Training Institute in Kanchipuram

  75. mohanraj says:

    You created an Impressive blog with lovely information.You made a really very useful article..Thanks for sharing such a wonderful blog...
    Best C++ Training Training Institute in Kanchipuram

  76. meenaraj 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…..…..No:1 Python Programming Training Academy in Kanchipuram

  77. karthik says:

    Hi, am a big follower of your blog. I am really happy to found such a helpful and fascinating post that is written in well manner. Thanks for sharing such an informative post. keep update your blog.
    Best UIPath Robotic Process Automation in Training Institute in Kanchipuram

  78. Awesome Post! I like writing style, the way you described the topics throughout the post. I hope many web reader will keep reading your post at the end, Thanks for sharing your view.No:1 Web Designing Training Academy in Kanchipuram

  79. Anu Ram 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.No:1 Python Programming Training Academy in Kanchipuram

  80. Impressive blog with lovely information. really very useful article for us thanks for sharing such a wonderful blog...
    Best Graphic designing Training Institute in Kanchipuram

  81. Thank you for taking the time to provide us with your valuable information. We strive to provide our candidates with excellent care and we take your comments to heart.As always, we appreciate your confidence and trust in us..Best Selenium Training Academy in Kanchipuram

  82. ramya raj says:

    Wonderful!! this is really one of the most beneficial blogs I’ve ever browsed on this subject. I am very glad to read such a great blog and thank you for sharing this good info with us.Keep posting stuff like this.Best AWS Training Academy in Kanchipuram

  83. mani says:

    Thanks for appreciating. Really means and inspires a lot to hear from you guys.I have bookmarked it and I am looking forward to reading new articles. Keep up the good work..Believe me, This is very helpful for me.
    Best dally erp9 Training Institute in Kanchipuram

  84. mani says:

    Thanks for appreciating. Really means and inspires a lot to hear from you guys.I have bookmarked it and I am looking forward to reading new articles. Keep up the good work..Believe me, This is very helpful for me.
    Best dally erp9 Training Institute in Kanchipuram

  85. Thanks for sharing these niche piece of knowledge. Here, I had a solution for my inconclusive problems & it’s really helps me a lot keep updates…No:1 Software Testing Training Academy in Kanchipuram

  86. karthik says:

    Wonderful article.It is to define the concepts very well.Clearly explain the information.It has more valuable information for encourage me to achieve my career goal.
    Best JAVA and J2EE Training Institute in Kanchipuram

  87. ramya raj says:

    Its really an Excellent post. I just stumbled upon your blog and wanted to say that I have really enjoyed reading your blog. Thanks for sharing…No:1 Python Programming Training Academy in Kanchipuram

  88. Really i enjoyed very much. And this may helpful for lot of peoples. So you are provided such a
    nice and great article within this.
    Best Web Designing and Development Training Institute in Kanchipuram

  89. It’s interesting that many of the bloggers to helped clarify a few things for me as Well as giving. Most of ideas can be nice content.…No:1 JAVA Training Academy in Kanchipuram

  90. mohanraj says:

    you done a great work,I’m really impressed with your article, such great & usefull knowledge you mentioned here.
    Best Graphic Designing & Multimedia Training Institute in Kanchipuram

  91. meenaraj says:

    Awesome Post! I like writing style, the way you described the topics throughout the post. I hope many web reader will keep reading your post at the end, Thanks for sharing your view.…No:1 Selenium Training Academy in Kanchipuram

Leave a Reply

Labels