Python Forum
collect show cdp neighbor output
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
collect show cdp neighbor output
#1
Hi

I hit a dead end here with my code. Not sure how i should proceed from this.

Description of my project:
I want to get a topology of my network. With the script i want to login to my main device, and then run a show cdp neighbor details command. With the output im getting i want to make a list of devices that is connected, and then it will connect to those devices one-by-one to do the same for them as for the main device, until there is no more devices. The return i want put in a .csv file.


The information i want is:
Device name
Device IP
Device model
The interfaces that connects the device with its parent device
If any VTP, i want that as well
Might want to add more to it later on. Serial no and software from show version maybe. (But thats for later)

The below code is where i login to a device and get the output from 2 neighbor switches. Im having trouble making new list or dictionary is problably better for each device.

import netmiko
import re
from functions import connect_switch

# Connect_switch is from another .py that login to the device
connect_switch()

device = connect_switch()

net_connect = netmiko.ConnectHandler(**device)
net_connect.enable()
command = net_connect.send_command('show cdp neighbors detail')
net_connect.disconnect()

output = command.split('\n')
print(output)

find_devices = [output.index(i) for i in output if 'Device ID:' in i]
find_ips = [output.index(i) for i in output if 'IP address:' in i]
find_ips = list(dict.fromkeys(find_ips)) #Had to add this line. To not have dublicate returns in my find_ips list. Without this it returns <class 'list'>: [3, 3, 25, 25]
find_platform = [output.index(i) for i in output if 'Platform: cisco ' in i]
find_src_int = [output.index(i) for i in output if 'Interface:' in i]
find_dst_int = [output.index(i) for i in output if '(outgoing port):' in i]
find_vtp = [output.index(i) for i in output if 'VTP Management Domain:' in i]

for i in find_devices:
    dev = re.match(r'Device ID: (.*)', output[i].rstrip(), re.M | re.I)
    if dev:
        device = dev.group(1)
        print('Device is: ' + device)

for i in find_ips:
    ip_adr = re.match(r'  IP address: (.*)', output[i].rstrip(), re.M | re.I)
    if ip_adr:
        ip_address = ip_adr.group(1)
        print('IP is: ' + ip_address)

# for some reason the find_platform returns <class 'list'>: [4, 4] where i would have expected [4, 26]
# Dont know if its because its the same platform on both devices
for i in find_platform:
    plat = re.match(r'(.*): (.*),', output[i].rstrip(), re.M | re.I)
    if plat:
        platform = plat.group(2)
        print('Remote Switch model is: ' + platform)

for i in find_src_int:
    int = re.match(r'Interface: (.*),  (.*) (.*) (.*)', output[i].rstrip(), re.M | re.I)
    if int:
        src_int = int.group(1)
        dst_int = int.group(4)
        print('Src Int is: ' + src_int)
        print('Dst Int is: ' + dst_int)

for i in find_vtp:
    vtp = re.match(r"VTP Management Domain: '(.*)'", output[i].rstrip(), re.M | re.I)
    if vtp:
        vtp_domain = vtp.group(1)
        print('VTP is: ' + vtp_domain)
The output from variable:output is here:
Output:
['-------------------------', 'Device ID: Device1', 'Entry address(es): ', ' IP address: 1.1.1.1', 'Platform: cisco WS-C3560CG-8PC-S, Capabilities: Switch IGMP ', 'Interface: GigabitEthernet0/10, Port ID (outgoing port): GigabitEthernet0/9', 'Holdtime : 172 sec', '', 'Version :', 'Cisco IOS Software, C3560C Software (C3560c405ex-UNIVERSALK9-M), Version 15.2(2)E7, RELEASE SOFTWARE (fc3)', 'Technical Support: http://www.cisco.com/techsupport', 'Copyright (c) 1986-2017 by Cisco Systems, Inc.', 'Compiled Wed 12-Jul-17 16:08 by prod_rel_team', '', 'advertisement version: 2', 'Protocol Hello: OUI=0x00000C, Protocol ID=0x0112; payload len=27, value=00000000FFFFFFFF010221FF000000000000BC16F5073B00FF0000', "VTP Management Domain: 'VTP1'", 'Native VLAN: 1', 'Duplex: full', 'Management address(es): ', ' IP address: 1.1.1.1', '', '-------------------------', 'Device ID: Device2', 'Entry address(es): ', ' IP address: 2.2.2.2', 'Platform: cisco WS-C3560CG-8PC-S, Capabilities: Switch IGMP ', 'Interface: GigabitEthernet0/9, Port ID (outgoing port): GigabitEthernet0/10', 'Holdtime : 136 sec', '', 'Version :', 'Cisco IOS Software, C3560C Software (C3560c405ex-UNIVERSALK9-M), Version 15.2(2)E7, RELEASE SOFTWARE (fc3)', 'Technical Support: http://www.cisco.com/techsupport', 'Copyright (c) 1986-2017 by Cisco Systems, Inc.', 'Compiled Wed 12-Jul-17 16:08 by prod_rel_team', '', 'advertisement version: 2', 'Protocol Hello: OUI=0x00000C, Protocol ID=0x0112; payload len=27, value=00000000FFFFFFFF010221FF000000000000A41875F29B00FF0000', "VTP Management Domain: 'VTP2'", 'Native VLAN: 1', 'Duplex: full', 'Management address(es): ', ' IP address: 2.2.2.2', '', '', 'Total cdp entries displayed : 2']
Output from Device, ip_address, platform, src_int, dst_int and vtp_domain
Output:
Device is: Device1 Device is: Device2 IP is: 1.1.1.1 IP is: 2.2.2.2 Remote Switch model is: cisco WS-C3560CG-8PC-S Remote Switch model is: cisco WS-C3560CG-8PC-S Src Int is: GigabitEthernet0/10 Dst Int is: GigabitEthernet0/9 Src Int is: GigabitEthernet0/9 Dst Int is: GigabitEthernet0/10 VTP is: VTP1 VTP is: VTP2
Hope you can help me out. :)
And comments on how the code is written is welcome as well, but most important for me is to get it working for now. Im still a rookie in python, so writing better code will come to me later im sure :)

Let me know if you need more info.

/ Carsten
Reply
#2
As you said, it would be better to use dictionary:

#!/usr/bin/python3
import pprint, re

output = [
 '-------------------------',
 'Device ID: Device1',
 'Entry address(es): ',
 '  IP address: 1.1.1.1',
 'Platform: cisco WS-C3560CG-8PC-S,  Capabilities: Switch IGMP ',
 'Interface: GigabitEthernet0/10,  Port ID (outgoing port): GigabitEthernet0/9',
 'Holdtime : 172 sec',
 '',
 'Version :',
 'Cisco IOS Software, C3560C Software (C3560c405ex-UNIVERSALK9-M), Version '
 '15.2(2)E7, RELEASE SOFTWARE (fc3)',
 'Technical Support: http://www.cisco.com/techsupport',
 'Copyright (c) 1986-2017 by Cisco Systems, Inc.',
 'Compiled Wed 12-Jul-17 16:08 by prod_rel_team',
 '',
 'advertisement version: 2',
 'Protocol Hello:  OUI=0x00000C, Protocol ID=0x0112; payload len=27, '
 'value=00000000FFFFFFFF010221FF000000000000BC16F5073B00FF0000',
 "VTP Management Domain: 'VTP1'",
 'Native VLAN: 1',
 'Duplex: full',
 'Management address(es): ',
 '  IP address: 1.1.1.1',
 '',
 '-------------------------',
 'Device ID: Device2',
 'Entry address(es): ',
 '  IP address: 2.2.2.2',
 'Platform: cisco WS-C3560CG-8PC-S,  Capabilities: Switch IGMP ',
 'Interface: GigabitEthernet0/9,  Port ID (outgoing port): GigabitEthernet0/10',
 'Holdtime : 136 sec',
 '',
 'Version :',
 'Cisco IOS Software, C3560C Software (C3560c405ex-UNIVERSALK9-M), Version '
 '15.2(2)E7, RELEASE SOFTWARE (fc3)',
 'Technical Support: http://www.cisco.com/techsupport',
 'Copyright (c) 1986-2017 by Cisco Systems, Inc.',
 'Compiled Wed 12-Jul-17 16:08 by prod_rel_team',
 '',
 'advertisement version: 2',
 'Protocol Hello:  OUI=0x00000C, Protocol ID=0x0112; payload len=27, '
 'value=00000000FFFFFFFF010221FF000000000000A41875F29B00FF0000',
 "VTP Management Domain: 'VTP2'",
 'Native VLAN: 1',
 'Duplex: full',
 'Management address(es): ',
 '  IP address: 2.2.2.2',
 '',
 '',
 'Total cdp entries displayed : 2'
]

device      = { }
device_list = [ ]
for line in output:
    if '-------------------------' in line:
        device       = { }
        device_list += [ device ]

    dev = re.match(r'Device ID: (.*)', line, re.M | re.I)
    if dev:
        device["id"] = dev.group(1)

    ip_adr = re.match(r'  IP address: (.*)', line, re.M | re.I)
    if ip_adr:
        device["ip"] = ip_adr.group(1)

    plat = re.match(r'(.*): (.*),', line, re.M | re.I)
    if plat:
        device["platform"] = plat.group(2)


pprint.pprint(device_list)
Output:
[{'id': 'Device1', 'ip': '1.1.1.1', 'platform': ' OUI=0x00000C, Protocol ID=0x0112; payload len=27'}, {'id': 'Device2', 'ip': '2.2.2.2', 'platform': ' OUI=0x00000C, Protocol ID=0x0112; payload len=27'}]
Reply
#3
Thanks Heiner55

That gave me just the result i wanted. :)
And i learned new stuff on how to make the code cleaner. I had a lot of code in mine that wasnt for any use now, and yours did the trick and looked prettier. :)

My next problem is adding the info together, so its easily to see what device is the main device, what device that is child to that, and add those info to it as well. I want to see topology wise in what order they come from the datacenter and out.

Might need some help for that as well. :)
Reply
#4
You are welcome.
Reply
#5
@carstenlp
Buddy, your code works like charm. Fantastic.
Can you also let me know how to save that data in excel sheet?

The query you posted to collect cdp neighbors details and then login to each neighbor and collect the same information, are you able to achieve that?

If yes, can you please share the code as i am as well trying to do the same. Cry
Reply


Forum Jump:

User Panel Messages

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