使用Exscript的Python Telnet连接

时间:2014-02-07 10:50:12

标签: python telnet python-2.6

我正在尝试使用此代码连接到思科防火墙:

 from Exscript.util.interact import read_login
 from Exscript.protocols import Telnet

 cmd = "?"
 conn = Telnet()
 conn.connect('10.60.2.254')

 conn.get_password_prompt()
 conn.send('password')
 conn.execute(cmd)
 print conn.response
 conn.send('exit\r')               
 conn.close()  

但是我收到了这个错误:

File "C:\Python26\Lib\site-packages\Pythonwin\pywin\framework\scriptutils.py", line 326, in RunScript exec codeObject in __main__.__dict__

File "C:\Users\blablabla\Desktop\ciscopytest.py", line 13, in <module>
    conn.execute(cmd)

File "C:\Python26\lib\site-packages\exscript-development-py2.6.egg\Exscript\protocols\Protocol.py", line 888, in execute
    return self.expect_prompt()

File "C:\Python26\lib\site-packages\exscript-development-py2.6.egg\Exscript\protocols\Protocol.py", line 987, in expect_prompt
    result = self.expect(self.get_prompt())

File "C:\Python26\lib\site-packages\exscript-development-py2.6.egg\Exscript\protocols\Protocol.py", line 968, in expect
    result = self._expect(prompt)

File "C:\Python26\lib\site-packages\exscript-development-py2.6.egg\Exscript\protocols\Protocol.py", line 944, in _expect
    result = self._domatch(to_regexs(prompt), True)

File "C:\Python26\lib\site-packages\exscript-development-py2.6.egg\Exscript\protocols\Telnet.py", line 85, in _domatch
    raise TimeoutException(error)

TimeoutException: Error while waiting for response from device

1 个答案:

答案 0 :(得分:0)

这可能会发生,因为?不是实际的命令。它显示帮助而不明确按Enter键。

尝试以下操作,而不是conn.execute(cmd)

conn.send(cmd)
self.expect_prompt()

如果这不起作用,请在手动按?

时粘贴设备的输出