Thursday, April 7, 2016

login to remote machines : pxssh

http://www.pythonforbeginners.com/code-snippets-source-code/ssh-connection-with-python
http://stackoverflow.com/questions/21055943/pxssh-connecting-to-an-ssh-proxy-timeout-exceeded-in-read-nonblocking

# Note: may get error as: TIMEOUT:
The issue is an out of date / absent snippet of code in /usr/lib/python.2.x/dist-packages/pxssh.py. (it may also be in usr/lib/python.2.x/site-packages/pxssh.py).

"
Navigate to this file and add the following lines of code:

self.sendline() #Line 134
time.sleep(0.5) #Line 135
This should be inserted just above the line

self.read_nonblocking(size=10000,timeout=1) # GAS: Clear out the cache before getting the prompt
"

# Code

#!/usr/bin/env python
import pxssh
import getpass
try:
    s = pxssh.pxssh()
    hostname = raw_input('hostname: ')
    username = raw_input('username: ')
    password = getpass.getpass('password: ')
    s.login (hostname, username, password)
    s.sendline ('uptime')   # run a command
    s.prompt()             # match the prompt
    print s.before          # print everything before the prompt.
    s.sendline ('ls -l')
    s.prompt()
    print s.before
    s.sendline ('df')
    s.prompt()
    print s.before
    s.logout()
except pxssh.ExceptionPxssh, e:
    print "pxssh failed on login."
    print str(e)
                                 

No comments:

Post a Comment