Test environment: Ubuntu12.04
Python-pexpect Installation: Apt-get install Python-pexpect
Test script:
#!/usr/bin/env pythonimport pxsshimport getpasstry: s = Pxssh.pxssh () hostname = raw_input (' hostname: ') Username = raw_input (' username: ') password = getpass.getpass ( ' please input password: ') s.login (hostname, username, Password) s.sendline (' uptime ') s.prompt () print s.before 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)
Error when executing:
Traceback (most recent):
File "pxsshsimple1.py", line 9, <module>
S.login (hostname, username, password)
File "/usr/lib/python2.7/dist-packages/pxssh.py", line 243, in login
If not self.synch_original_prompt ():
File "/usr/lib/python2.7/dist-packages/pxssh.py", line 134, in Synch_original_prompt
Self.read_nonblocking (size=10000,timeout=1) # Gas:clear out the cache before getting the prompt
File "/usr/lib/python2.7/dist-packages/pexpect.py", line 824, in read_nonblocking
Raise timeout (' timeout exceeded in read_nonblocking (). ')
Pexpect. Timeout:timeout exceeded in read_nonblocking ().
Modify the/usr/lib/python2.7/dist-packages/pxssh.py file to add before the line134 line:
Self.sendline () time.sleep (0.5)
PXSSH Remote execution Command error handling