Python:通过 SSH 连接到 Cisco 设备并运行 show 命令
我已经广泛阅读了这篇文章,并研究了 Exscript、paramiko、Fabric 和 pxssh,但我仍然迷失与 Cisco 路由器的持久 ssh 会话。我是 python 脚本编写的新手。
我正在尝试用 Python 编写一个脚本,该脚本将通过 SSH 连接到 Cisco 设备,运行“show version”,在记事本中显示结果,然后结束脚本。
我可以使用不需要用户与设备交互的 show 命令来实现这一点。例如:
from Exscript.util.interact import read_login
from Exscript.protocols import SSH2
account = read_login()
conn = SSH2()
conn.connect('192.168.1.11')
conn.login(account)
conn.execute('show ip route')
print conn.response
conn.send('exit\r')
conn.close()
上面的脚本将显示“show ip route”的结果。
如果我尝试 conn.execute('show version') 脚本会超时,因为 Cisco 设备希望用户按空格键继续,按回车键显示下一行或按任意键返回命令行。
如何执行show version命令,按两次空格键显示show version命令的整个输出,然后在python中打印它?
谢谢你!!!!
I have read over this post extensively and have researched Exscript, paramiko, Fabric and pxssh and I am still lost Persistent ssh session to Cisco router . I am new to python scripting.
I am attempting to write a script in Python that will SSH into a Cisco device, run "show version", display the results in notepad, then end the script.
I can get this working with show commands that do not require the user to interact with the device. For example:
from Exscript.util.interact import read_login
from Exscript.protocols import SSH2
account = read_login()
conn = SSH2()
conn.connect('192.168.1.11')
conn.login(account)
conn.execute('show ip route')
print conn.response
conn.send('exit\r')
conn.close()
The above script will display the results of "show ip route".
If I try conn.execute('show version') the script times out because the Cisco device is expecting the user to press space bar to continue, press return to show the next line or any key to back out to the command line.
How can I execute the show version command, press space bar twice to display the entire output of the show version command, then print it in python?
Thank you!!!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在运行
show version
之前尝试执行terminal length 0
。例如:从 Cisco 终端文档: http://www.cisco.com/en/US/docs/ios/12_1/configfun/command/reference/frd1003.html#wp1019281
Try executing
terminal length 0
before runningshow version
. For example:From the Cisco terminal docs: http://www.cisco.com/en/US/docs/ios/12_1/configfun/command/reference/frd1003.html#wp1019281
首先执行
禁用分页。
First execute
to disable paging.
我刚刚问了同样的问题,下面的代码将从列表中运行并获取您所要求的信息。
I just asked the same thing and the below code will run from a list and obtain the information you are asking for.