程式扎記: [ Python 文章收集 ] SSH Connection with Python

標籤

2015年7月9日 星期四

[ Python 文章收集 ] SSH Connection with Python

Source From Here
Overview
Last week I wrote an article about the pexpect module in Python and how you can use it to take care of some of the automation needs, like ssh and ftp. I would like to continue on that topic and write about it's pxssh module:
This class extends pexpect.spawn to specialize setting up SSH connections.
This adds methods for loginlogout, and expecting the shell prompt.

With the pxssh module, it's easy to access other servers over SSH.

Module documentation
Open up a terminal and type in the following commands to get help about the module
>>> from pexpect import pxssh
>>> help(pxssh)



You can also see the help here http://pexpect.sourceforge.net/pxssh.html

Methods and login process
Pxssh adds methods for login, logout, and expecting the shell prompt.

It does various tricky things to handle many situations in the SSH login process. For example, if the session is your first login, then pxssh automatically accepts the remote certificate; or if you have public key authentication setup then pxssh won't wait for the password prompt.

pxssh uses the shell prompt to synchronize output from the remote host. In order to make this more robust it sets the shell prompt to something more unique than just $ or #. This should work on most Borne/Bash or Csh style shells.

Example
Below example runs a few commands on a remote server and prints the result. First we import the modules that we need. (pxssh and getpass). Then we usegetpass module to prompt the user for a password, without echoing what they type to the console.
  1. import pxssh  
  2. import getpass  
  3. try:                                                              
  4.     s = pxssh.pxssh()  
  5.     hostname = raw_input('hostname: ')  
  6.     username = raw_input('username: ')  
  7.     password = getpass.getpass('password: ')  
  8.     s.login (hostname, username, password)  
  9.     s.sendline ('uptime')   # run a command  
  10.     s.prompt()             # match the prompt  
  11.     print s.before          # print everything before the prompt.  
  12.     s.sendline ('ls -l')  
  13.     s.prompt()  
  14.     print s.before  
  15.     s.sendline ('df')  
  16.     s.prompt()  
  17.     print s.before  
  18.     s.logout()  
  19. except pxssh.ExceptionPxssh, e:  
  20.     print "pxssh failed on login."  
  21.     print str(e)  
Let's show one more example. To run a command ('uptime') and to print the output, you need to do something like that :
  1. import pxssh  
  2. s = pxssh.pxssh()  
  3. if not s.login ('localhost''myusername''mypassword'):  
  4.     print "SSH session failed on login."  
  5.     print str(s)  
  6. else:  
  7.     print "SSH session login successful"  
  8.     s.sendline ('uptime')  
  9.     s.prompt()         # match the prompt  
  10.     print s.before     # print everything before the prompt.  
  11.     s.logout()  
  12.       
  13. #We can also execute multiple command like this:  
  14. s.sendline ('uptime;df -h')  


沒有留言:

張貼留言

網誌存檔

關於我自己

我的相片
Where there is a will, there is a way!