Python Forum
ciscolib cdp output list printing support
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
ciscolib cdp output list printing support
#1
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
Reply
#2
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...
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
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
Reply
#4
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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  sum() list from SQLAlchemy output Personne 5 4,462 May-17-2022, 12:25 AM
Last Post: Personne
Question Printing through list.. again jesse68 2 1,142 Apr-16-2022, 03:24 PM
Last Post: jesse68
  help for list printing jip31 8 3,636 May-01-2021, 03:52 AM
Last Post: Pedroski55
  Problem printing last element from a list tester_V 3 2,407 Oct-30-2020, 04:54 AM
Last Post: tester_V
  Printing empty list? hhydration 2 2,123 Oct-28-2020, 11:34 AM
Last Post: Atekka
  How to append to list a function output? rama27 5 6,728 Aug-24-2020, 10:53 AM
Last Post: DeaD_EyE
  Printing images from a list Heyjoe 4 2,830 Jun-22-2020, 02:28 AM
Last Post: Heyjoe
  json.dumps list output qurr 12 5,189 Apr-08-2020, 10:13 PM
Last Post: micseydel
  If item in list = true, Output = xx kroh 0 1,491 Feb-19-2020, 09:17 AM
Last Post: kroh
  printing a list contents without brackets in a print statement paracelx 1 2,129 Feb-15-2020, 02:15 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020