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

Discussão em 'Python' iniciado por Stack, Dezembro 20, 2024.

  1. Stack

    Stack Membro Participativo

    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

    Continue reading...

Compartilhe esta Página