Python Forum
collect show cdp neighbor output
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
collect show cdp neighbor output
#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


Messages In This Thread
collect show cdp neighbor output - by carstenlp - May-21-2019, 07:00 AM
RE: collect show cdp neighbor output - by heiner55 - May-22-2019, 08:42 AM
RE: collect show cdp neighbor output - by carstenlp - May-22-2019, 11:02 AM
RE: collect show cdp neighbor output - by heiner55 - May-29-2019, 02:31 AM
RE: collect show cdp neighbor output - by Ricky84 - Jan-30-2020, 11:19 AM

Forum Jump:

User Panel Messages

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