Paramiko exec_command with multiple commands on Cisco router not providing any output

两盒软妹~` 提交于 2020-12-08 04:18:33

问题


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

Above is my code snippet, passing just one command prints the results just fine, however with multiple commands as above, it does not work (no output). The device being logged onto is a Cisco ASR1006 - Does it have anything to do with the device?

Any help is much appreciated!


Since multiple commands didn't work, I'm using below code with multiple exec_command, but this this takes about 30-40 secs each execution.. ssh.connect needed for each command execution?

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()

As suggested in comments, I have also tried to call exec_command multiple times over one connection:

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()

But that's failing with:

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

回答1:


(At least some) Cisco routers are known not to support multiple commands in one "exec" request: https://the.earth.li/~sgtatham/putty/latest/htmldoc/Chapter3.html#using-cmdline-m

With some servers (particularly Unix systems), you can even put multiple lines in this file and execute more than one command in sequence, or a whole shell script; but this is arguably an abuse, and cannot be expected to work on all servers. In particular, it is known not to work with certain ‘embedded’ servers, such as Cisco routers.


Normally, you could run exec_command multiple times over the same connection. But that does not seem possible with Cisco either.

Then I'd recommends you to stick with reopening the session for each command. While inefficient, its a reliable approach.


If the inefficiency is not acceptable, you can use a shell channel like:

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

But that's not reliable. It is difficult tell when an output of one command finishes and the other starts. It is also difficult tell, if you got all output already or not. And as Paramiko always uses PTY, you get a lot of unwanted output, like prompts, command echoes, etc.



来源:https://stackoverflow.com/questions/48229181/paramiko-exec-command-with-multiple-commands-on-cisco-router-not-providing-any-o

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!