使用pxssh时读取非阻塞错误

时间:2014-05-03 06:30:46

标签: python ssh pexpect

我正在尝试使用pxssh moudule使用python脚本在我的机器上执行ssh。我降落在

    Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.6/dist-packages/pxssh.py", line 243, in login
    if not self.synch_original_prompt():
  File "/usr/local/lib/python2.6/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/local/lib/python2.6/dist-packages/pexpect.py", line 824, in read_nonblocking
    raise TIMEOUT ('Timeout exceeded in read_nonblocking().')
pexpect.TIME

OUT: Timeout exceeded in read_nonblocking().

之后我提到了

  

Read Non Blocking error while using pxssh (ssh module for python)

并遵循第二个解决方案

  

&#34;我刚刚在之前添加了self.sendline()和time.sleep(0.5)   首先在synch_original_prompt()&#34;

中调用read_nonblocking()

执行此操作后,我的超时错误已解决,但我得到的输出与我给出的命令相同。我尝试打印出child.before和child.after

print child.before
print child.after 

输出

ls -l
<class 'pexpect.TIMEOUT'>

这是我的完整代码

import pxssh

def send_command(child,cmd):
    child.send(cmd)
    child.prompt()
    print child.before
    print child.after

def connect(host,user,pwd):   
    s = pxssh.pxssh()
    s.login(host,user,pwd,auto_prompt_reset=True)
    return s

def main():
    s = connect('127.0.0.1','root','*********')
    send_command(s, "ls -l")

if __name__ == "__main__":
    main()

0 个答案:

没有答案