2014-01-10 93 views
1

所以我已经尝试了几个模块pxssh,pexpect等,我似乎无法获得ssh连接到我们的ssh代理工作。基本上我想做的是使用我们的SSH代理,它可以访问我们其余的设备作为其他设备的跳转点,然后运行命令。这工作在ipython中,但是当它连接时会引发下面列出的异常,但是它继续执行其余的命令并且工作正常。但是,当只是作为python脚本运行时,它只会导致脚本失败。由于例外,我认为这是失败的。有没有办法关闭这个例外,或者避免这种例外?还是我这样做全错了?pxssh连接到一个ssh代理; read_nonblocking()中超时超时

我也试图与提示设置和auto_prompt_reset设置搞乱......

代理主机是不是一个真正的Linux主机,或Cisco对于这个问题,这可能是为什么违约设置不工作。

不确定该从哪里出发,所以任何帮助或见解都会很棒。

import pxssh 
s = pxssh.pxssh() 
hostname = '10.10.10.4' 
username = 'username' 
password = 'password!' 
s.login (hostname, username, password) 
s.sendline ('connect 10.10.5.1') 
s.prompt(timeout=2)  
print s.before 
s.endline ('') 
s.sendline ('show run') 
s.prompt(timeout=2) 
print s.before 

发布的异常。

In [40]: s.login (hostname, username, password) 
--------------------------------------------------------------------------- 
TIMEOUT         Traceback (most recent call last) 
<ipython-input-40-51f00f2075f5> in <module>() 
----> 1 s.login (hostname, username, password) 

/usr/lib/python2.7/dist-packages/pxssh.pyc in login(self, server, username, password, terminal_type, original_prompt, login_timeout, port, auto_prompt_reset) 
    241    self.close() 
    242    raise ExceptionPxssh ('unexpected login response') 
--> 243   if not self.synch_original_prompt(): 
    244    self.close() 
    245    raise ExceptionPxssh ('could not synchronize with original prompt') 

/usr/lib/python2.7/dist-packages/pxssh.pyc in synch_original_prompt(self) 
    132   # If latency is worse than these values then this will fail. 
    133 
--> 134   self.read_nonblocking(size=10000,timeout=1) # GAS: Clear out the cache  before getting the prompt 
    135   time.sleep(0.1) 
    136   self.sendline() 

/usr/lib/python2.7/dist-packages/pexpect.pyc in read_nonblocking(self, size, timeout) 
    822     raise EOF ('End of File (EOF) in read_nonblocking(). Very pokey  platform.') 
    823    else: 
--> 824     raise TIMEOUT ('Timeout exceeded in read_nonblocking().') 
    825 
    826   if self.child_fd in r: 

TIMEOUT: Timeout exceeded in read_nonblocking(). 

回答

3

研究这个为自己之后,我碰到#2的另一篇文章中,提问者发布他们的解决方案。我测试了解决方案,并且它也适用于我自己。这个问题是usr/lib/python.2.6/dist-packages/pxssh.py中过时/缺少的代码片段。 (它也可能在usr/lib/python.2.6/site-packages/pxssh.py中)。

导航到该文件,并添加以下代码行:

self.sendline() #Line 134 
time.sleep(0.5) #Line 135 

这应该只是上面一行

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

和中提琴被插入,再次运行你的代码,它应该像魅力。 贷:python pexpect: SSHing then updating the date