我已经广泛阅读了这篇文章并研究了Exscript,paramiko,Fabric和pxssh,我仍然迷失了Persistent ssh session to Cisco router。我是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中打印?
谢谢!!!!
答案 0 :(得分:18)
在运行terminal length 0
之前尝试执行show version
。例如:
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('terminal length 0')
conn.execute('show version')
print conn.response
conn.send('exit\r')
conn.close()
来自思科终端文档:http://www.cisco.com/en/US/docs/ios/12_1/configfun/command/reference/frd1003.html#wp1019281
答案 1 :(得分:7)
首先执行
terminal length 0
禁用分页。
答案 2 :(得分:2)
我刚刚问了同样的事情,下面的代码将从列表中运行并获取您要求的信息。
from __future__ import print_function
from netmiko import ConnectHandler
import sys
import time
import select
import paramiko
import re
fd = open(r'C:\NewdayTest.txt','w') # Where you want the file to save to.
old_stdout = sys.stdout
sys.stdout = fd
platform = 'cisco_ios'
username = 'username' # edit to reflect
password = 'password' # edit to reflect
ip_add_file = open(r'C:\IPAddressList.txt','r') # a simple list of IP addresses you want to connect to each one on a new line
for host in ip_add_file:
host = host.strip()
device = ConnectHandler(device_type=platform, ip=host, username=username, password=password)
output = device.send_command('terminal length 0')
output = device.send_command('enable') #Editable to be what ever is needed
print('##############################################################\n')
print('...................CISCO COMMAND SHOW RUN OUTPUT......................\n')
output = device.send_command('sh run')
print(output)
print('##############################################################\n')
print('...................CISCO COMMAND SHOW IP INT BR OUTPUT......................\n')
output = device.send_command('sh ip int br')
print(output)
print('##############################################################\n')
fd.close()