Python Forum

Full Version: ciscolib cdp output list printing support
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi There,

trying ciscolib module to discover network topology. small script as below

import ciscolib
switch = ciscolib.Device("172.21.160.3", "test123", "anna")
switch.connect()
print(switch.get_neighbors())
Output:
[{'ip': '172.21.162.164', 'hostname': 'PUN-DIG-DIG-01-ONN-BR-BS-GL-01', 'remote_port': 'FastEthernet0/24', 'local_port': 'FastEthernet0/18'}, {'ip': '172.21.162.157', 'hostname': 'PUN-DIG-DIG-02-ONN-BR-BS-01', 'remote_port': 'FastEthernet0/24', 'local_port': 'FastEthernet0/15'}, {'ip': '172.21.162.71', 'hostname': 'PUN-DIG-DIG-01-ONN-BR-BS-70', 'remote_port': 'FastEthernet0/24', 'local_port': 'FastEthernet0/19'}, {'ip': '172.21.160.2', 'hostname': 'pu-dig-dig-r1-as01', 'remote_port': 'GigabitEthernet1/0/9', 'local_port': 'GigabitEthernet0/1'}]
need to print output as below

ip:172.21.162.164 hostname:PUN-DIG-DIG-01-ONN-BR-BS-GL-01 remote_port:FastEthernet0/24 local_port:FastEthernet0/18
ip:172.21.162.157 hostname:PUN-DIG-DIG-02-ONN-BR-BS-01 remote_port:FastEthernet0/24 local_port:FastEthernet0/15
ip:172.21.160.2 hostname:pu-dig-dig-r1-as01 remote_port:GigabitEthernet1/0/9 local_port:GigabitEthernet0/1
you get list of dicts. Iterate over it and print elements whatever you like.
Frankly, it becomes boring to answer virtually same questions over and over again...
thanks, Buran, sorry for such boring question... I am not full time coder and weak at list and dict.

Hi Buran,

is this ok? please suggest



import ciscolib
ip = "172.21.160.3"
switch = ciscolib.Device(ip, "test123", "anna")
switch.connect()
ne= switch.get_neighbors()
for item in ne:
    print ip+"\t"+item['ip']+"\t"+item['hostname']+"\t"+item['remote_port']+"\t"+item['local_port']
Output:
172.21.160.3 172.21.162.164 PUN-DIG-DIG-01-ONN-BR-BS-GL-01 FastEthernet0/24 FastEthernet0/18 172.21.160.3 172.21.162.157 PUN-DIG-DIG-02-ONN-BR-BS-01 FastEthernet0/24 FastEthernet0/15 172.21.160.3 172.21.162.71 PUN-DIG-DIG-01-ONN-BR-BS-70 FastEthernet0/24 FastEthernet0/19 172.21.160.3 172.21.160.2 pu-dig-dig-r1-as01 GigabitEthernet1/0/9 GigabitEthernet0/1
use string
(Jul-25-2018, 11:18 AM)anna Wrote: [ -> ]is this ok?
well, that is a question only you can answer. Does it look like YOU want it?
for print you may use something like this, more pythonic
neighbours = [{'ip': '172.21.162.164', 'hostname': 'PUN-DIG-DIG-01-ONN-BR-BS-GL-01', 'remote_port': 'FastEthernet0/24', 'local_port': 'FastEthernet0/18'}, {'ip': '172.21.162.157', 'hostname': 'PUN-DIG-DIG-02-ONN-BR-BS-01', 'remote_port': 'FastEthernet0/24', 'local_port': 'FastEthernet0/15'}, {'ip': '172.21.162.71', 'hostname': 'PUN-DIG-DIG-01-ONN-BR-BS-70', 'remote_port': 'FastEthernet0/24', 'local_port': 'FastEthernet0/19'}, {'ip': '172.21.160.2', 'hostname': 'pu-dig-dig-r1-as01', 'remote_port': 'GigabitEthernet1/0/9', 'local_port': 'GigabitEthernet0/1'}]
my_ip = "172.21.160.3"
for item in neighbours:
    print('{my_ip}\t{ip}\t{hostname: <30}\t{remote_port}\t{local_port}'.format(my_ip=my_ip, **item))
or use some of the avalable packages for print tabular data:
https://stackoverflow.com/a/26937531/4046632