Search code examples
listpython-2.7foreachparamikoenumerate

Python List - For x in list not Passing to loop


When I try to do something for each item in a list it doesn't work. In each "sh ip route + x + \n" It should be using each item in the list (an IP) in place of x.

I even tried printing before and after the actual using of the code in paramiko's channel send to make sure that the variable is passed to x. Yet each "sh ip route x" command when using x, only uses the first item in the list.

sla_route_ips = ['10.0.0.1', '10.2.0.1', '10.3.0.1', '10.4.0.1', '10.5.0.1']

Command:

pprint.pprint(sla_route_ips)

for x in sla_route_ips:
    print x
    router_channel.send('sh ip route ' + x + '\n')
    print x
    while not buff.endswith('#'):
        resp = router_channel.recv(999999)
        buff += resp
    print buff

Output:

C:\Python27\Scripts>buffswith2.py
Enter Full Router ID: Router-VPN
['10.0.0.1', '10.2.0.1', '10.3.0.1', '10.4.0.1', '10.5.0.1']
10.0.0.1
10.0.0.1
sh ip route 10.0.0.1
Routing entry for 10.0.0.0/24
  Known via "bgp 1976", distance 200, metric 0, type internal
  Last update from 10.8.111.17 2w6d ago
  Routing Descriptor Blocks:
  * 10.8.111.17, from 10.8.0.1, 2w6d ago
      Route metric is 0, traffic share count is 1
      AS Hops 0

Router-VPN#
10.2.0.1
10.2.0.1
sh ip route 10.0.0.1
Routing entry for 10.0.0.0/24
  Known via "bgp 1976", distance 200, metric 0, type internal
  Last update from 10.8.111.17 2w6d ago
  Routing Descriptor Blocks:
  * 10.8.111.17, from 10.8.0.1, 2w6d ago
      Route metric is 0, traffic share count is 1
      AS Hops 0

Router-VPN#
10.3.0.1
10.3.0.1
sh ip route 10.0.0.1
Routing entry for 10.0.0.0/24
  Known via "bgp 1976", distance 200, metric 0, type internal
  Last update from 10.8.111.17 2w6d ago
  Routing Descriptor Blocks:
  * 10.8.111.17, from 10.8.0.1, 2w6d ago
      Route metric is 0, traffic share count is 1
      AS Hops 0

Router-VPN#
10.4.0.1
10.4.0.1
sh ip route 10.0.0.1
Routing entry for 10.0.0.0/24
  Known via "bgp 1976", distance 200, metric 0, type internal
  Last update from 10.8.111.17 2w6d ago
  Routing Descriptor Blocks:
  * 10.8.111.17, from 10.8.0.1, 2w6d ago
      Route metric is 0, traffic share count is 1
      AS Hops 0

Router-VPN#
10.5.0.1
10.5.0.1
sh ip route 10.0.0.1
Routing entry for 10.0.0.0/24
  Known via "bgp 1976", distance 200, metric 0, type internal
  Last update from 10.8.111.17 2w6d ago
  Routing Descriptor Blocks:
  * 10.8.111.17, from 10.8.0.1, 2w6d ago
      Route metric is 0, traffic share count is 1
      AS Hops 0

Router-VPN#

Desired Output:

C:\Python27\Scripts>buffswith2.py
Enter Full Router ID: Router-VPN
['10.0.0.1', '10.2.0.1', '10.3.0.1', '10.4.0.1', '10.5.0.1']
10.0.0.1
10.0.0.1
sh ip route 10.0.0.1
Routing entry for 10.0.0.0/24
  Known via "bgp 1976", distance 200, metric 0, type internal
  Last update from 10.8.111.17 2w6d ago
  Routing Descriptor Blocks:
  * 10.8.111.17, from 10.8.0.1, 2w6d ago
      Route metric is 0, traffic share count is 1
      AS Hops 0

Router-VPN#
10.2.0.1
10.2.0.1
sh ip route 10.2.0.1
Routing entry for 10.2.0.0/24
  Known via "bgp 1976", distance 200, metric 0, type internal
  Last update from 10.8.111.17 2w6d ago
  Routing Descriptor Blocks:
  * 10.8.111.17, from 10.8.0.1, 2w6d ago
      Route metric is 0, traffic share count is 1
      AS Hops 0

Router-VPN#
10.3.0.1
10.3.0.1
sh ip route 10.3.0.1
Routing entry for 10.3.0.0/24
  Known via "bgp 1976", distance 200, metric 0, type internal
  Last update from 10.8.111.17 2w6d ago
  Routing Descriptor Blocks:
  * 10.8.111.17, from 10.8.0.1, 2w6d ago
      Route metric is 0, traffic share count is 1
      AS Hops 0

Router-VPN#
10.4.0.1
10.4.0.1
sh ip route 10.4.0.1
Routing entry for 10.4.0.0/24
  Known via "bgp 1976", distance 200, metric 0, type internal
  Last update from 10.8.111.17 2w6d ago
  Routing Descriptor Blocks:
  * 10.8.111.17, from 10.8.0.1, 2w6d ago
      Route metric is 0, traffic share count is 1
      AS Hops 0

Router-VPN#
10.5.0.1
10.5.0.1
sh ip route 10.5.0.1
Routing entry for 10.5.0.0/24
  Known via "bgp 1976", distance 200, metric 0, type internal
  Last update from 10.8.111.17 2w6d ago
  Routing Descriptor Blocks:
  * 10.8.111.17, from 10.8.0.1, 2w6d ago
      Route metric is 0, traffic share count is 1
      AS Hops 0

Router-VPN#

UPDATE:

Running the command with it OUTSIDE of the paramiko router_channel.send() creates the desired output for the commands.

Command:

for x in sla_route_ips:
    print x
    print 'sh ip route ' + x 
    print x

C:\Python27\Scripts>buffswith2.py
Enter Full Router ID: Router-VPN
['10.0.0.1', '10.2.0.1', '10.3.0.1', '10.4.0.1', '10.5.0.1']
sh ip route 10.0.0.1
sh ip route 10.2.0.1
sh ip route 10.3.0.1
sh ip route 10.4.0.1
sh ip route 10.5.0.1

This tells me their is some type of error in handing off the command to Paramiko.


Solution

  • Python list iteration works fine. Your problem is that you do not reset variable buff before you try to read back the router response. As a result, your program never reads the second and subsequent responses because buff already satisfies the condition of the inner loop. The first response is simply output again.