Tokyo at NightI've been in the situation before were I needed to discover the neighbors of a Cisco device programmatically. This is useful in a case where you want to crawl a network without hard-coding a list of devices into your script. By default, all Cisco devices have CDP enabled and we can use this to get our neighbor list.

The first thing you'll need is the CDP MIB - CISCO-CDP-MIB. If you have the MIB and Net-SNMP package installed, you should be able to issue the following command on the command line:

snmpwalk -mCISCO-CDP-MIB -v2c -c [RO_community] [hostname] ciscoCdpMIB

This should dump a lot of details to your terminal. Here's a snippet of output from my Cisco 2611XM. In all there were 31 lines of output printed.

CISCO-CDP-MIB::cdpInterfaceEnable.1 = INTEGER: true(1)
CISCO-CDP-MIB::cdpInterfaceEnable.2 = INTEGER: true(1)
CISCO-CDP-MIB::cdpInterfaceEnable.3 = INTEGER: true(1)
CISCO-CDP-MIB::cdpInterfaceMessageInterval.1 = INTEGER: 0 seconds
CISCO-CDP-MIB::cdpInterfaceMessageInterval.2 = INTEGER: 0 seconds
CISCO-CDP-MIB::cdpInterfaceMessageInterval.3 = INTEGER: 0 seconds
CISCO-CDP-MIB::cdpInterfaceGroup.1 = INTEGER: 0
CISCO-CDP-MIB::cdpInterfaceGroup.2 = INTEGER: 0
CISCO-CDP-MIB::cdpInterfaceGroup.3 = INTEGER: 0
CISCO-CDP-MIB::cdpInterfacePort.1 = INTEGER: 0
CISCO-CDP-MIB::cdpInterfacePort.2 = INTEGER: 0
CISCO-CDP-MIB::cdpInterfacePort.3 = INTEGER: 0
CISCO-CDP-MIB::cdpInterfaceEntry.6.1 = STRING: "FastEthernet0/0"
CISCO-CDP-MIB::cdpInterfaceEntry.6.2 = STRING: "FastEthernet0/1"
CISCO-CDP-MIB::cdpInterfaceEntry.6.3 = STRING: "Serial0/0"

The two most important OIDs in terms of finding neighbors are cdpCacheDeviceId, which gives the device name, and cdpCacheAddress, which gives the IP address. The latter is extremely useful if you need to contact the neighbor and your device naming scheme does not match up to DNS.

Now you just need to take the above information and apply it in your programming language. Let me know how this works out. I'd love to hear from you!

Recommended Reading:

Network Warrior
Network Warrior
Essential SNMP
Essential SNMP