因此,我尝试了几个模块,包括pxssh、pexpect等,但似乎无法将ssh连接到我们的ssh代理。基本上,我想要做的是使用我们的SSH代理,它可以访问我们的其余设备作为其他设备的跳跃点,然后运行一个命令。这在ipython中可以工作,但是当它连接时,它会引发下面列出的异常,但是它会继续使用其他命令,并且工作得很好。但是,当只是作为python脚本运行时,它只会导致脚本失败。我会假设它是因为异常而失败的。有没有办法关闭这个异常,或者阻止它跳出呢?还是说我做错了?
我也尝试过修改提示符设置和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().发布于 2014-05-13 07:20:08
在自己研究了这个之后,我在Stackoverflow上看到了另一篇文章,在这篇文章中,发问者发布了他们的解决方案。我测试了这个解决方案,它也适用于我自己。该问题是usr/lib/python.2.6/dist-package/pxssh.py中的一个过期/缺失的代码片段。(它也可能在usr/lib/python.2.6/site-package/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还有viola,再运行一次你的代码,它就会像魔咒一样工作。致谢:python pexpect: SSHing then updating the date
https://stackoverflow.com/questions/21055943
复制相似问题