login_user = 'xyz'
login_pass = 'xyz'
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(device, username=login_user, password=login_pass, look_for_keys=False, timeout=5)
print "success loggedin"
stdin, stdout, stderr = ssh.exec_command("term len 0 ; show int desc | i Tu ; show ip interface brief | in Tunnel ; show ip bgp vpnv4 vrf AWS summary | i 169.25 ; show ip route vrf AWS bgp")
all_output = stdout.read()
print all_output
上面是我的代码片段,只传递一个命令打印结果就好了,但是如上面的多个命令,它不起作用(没有输出)。登录的设备是Cisco ASR1006 - 它与设备有什么关系吗?
非常感谢任何帮助!
由于多个命令不起作用,我使用下面的代码和多个exec_command,但是每次执行大约需要30-40秒。 每个命令执行需要ssh.connect吗?
ssh.connect(device, username=login_user, password=login_pass, look_for_keys=False)
stdin, stdout, stderr = ssh.exec_command('show int desc | i Tu')
vpc_ints = stdout.read()
ssh.connect(device, username=login_user, password=login_pass, look_for_keys=False)
stdin, stdout, stderr = ssh.exec_command('show ip interface brief | in Tunnel')
vpc_peers = stdout.read()
ssh.connect(device, username=login_user, password=login_pass, look_for_keys=False)
stdin, stdout, stderr = ssh.exec_command('show ip bgp vpnv4 vrf AWS summary | i 169.25')
vpc_bgp_routes = stdout.read()
ssh.connect(device, username=login_user, password=login_pass, look_for_keys=False)
stdin, stdout, stderr = ssh.exec_command('show ip route vrf AWS bgp')
ip_routes_list = stdout.read()
正如评论中所建议的那样,我也试图通过一个连接多次致电exec_command
:
ssh.connect(device, username=login_user, password=login_pass, look_for_keys=False)
stdin, stdout, stderr = ssh.exec_command('show int desc | i Tu')
vpc_ints = stdout.read()
#ssh.connect(device, username=login_user, password=login_pass, look_for_keys=False)
stdin, stdout, stderr = ssh.exec_command('show ip interface brief | in Tunnel')
vpc_peers = stdout.read()
#ssh.connect(device, username=login_user, password=login_pass, look_for_keys=False)
stdin, stdout, stderr = ssh.exec_command('show ip bgp vpnv4 vrf AWS summary | i 169.25')
vpc_bgp_routes = stdout.read()
#ssh.connect(device, username=login_user, password=login_pass, look_for_keys=False)
stdin, stdout, stderr = ssh.exec_command('show ip route vrf AWS bgp')
ip_routes_list = stdout.read()
ssh.close()
但那失败了:
File "C:\Python27\myvpndashboard\myvpnapp\tunnel_summary.py", line 179, in dataset_build
cisco_show(device)
File "C:\Python27\myvpndashboard\myvpnapp\tunnel_summary.py", line 36, in cisco_show
stdin, stdout, stderr = ssh.exec_command('show ip interface brief | in Tunnel')
File "C:\Users\sduraisami\Envs\myvpndashboard\lib\site-packages\paramiko\client.py", line 472, in exec_command
chan = self._transport.open_session(timeout=timeout)
File "C:\Users\sduraisami\Envs\myvpndashboard\lib\site-packages\paramiko\transport.py", line 765, in open_session
timeout=timeout)
File "C:\Users\sduraisami\Envs\myvpndashboard\lib\site-packages\paramiko\transport.py", line 889, in open_channel
raise e
EOFError
答案 0 :(得分:0)
(至少一些)已知Cisco路由器不支持一个“exec”请求中的多个命令: https://the.earth.li/~sgtatham/putty/latest/htmldoc/Chapter3.html#using-cmdline-m
对于某些服务器(特别是Unix系统),您甚至可以在此文件中放置多行,按顺序执行多个命令,或整个shell脚本; 但这可能是滥用行为,并且无法在所有服务器上运行。特别是,众所周知,不能使用某些“嵌入式”服务器,例如Cisco路由器。
通常,您可以在同一连接上多次运行exec_command
。但思科似乎也无法做到这一点。
然后我建议你坚持为每个命令重新打开会话。虽然效率低下,但却是一种可靠的方法。
如果低效率不可接受,您可以使用shell通道,如:
channel = ssh.invoke_shell()
channel.send('command 1\n')
channel.send('command 2\n')
channel.send('command 3\n')
while not channel.recv_ready():
time.sleep(3)
out = channel.recv(9999)
print out
但这不可靠。很难判断一个命令的输出何时结束而另一个命令的输出开始。如果您已经或现在已经获得所有输出,也很难说。由于Paramiko总是使用PTY,你会得到很多不需要的输出,如提示,命令回声等。