Skip to content

Instantly share code, notes, and snippets.

Created July 12, 2013 05:02
  • Star 10 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
A small python script for cdp device discovery
#! /usr/bin/env python
# A small script for cdp devices discovery
import sys
import pcapy
import socket
from dpkt import ethernet
from dpkt import cdp
from docopt import docopt
__doc__ = """Usage: %s [-h] <interface>
Listens on interface to discover devices emitting cdp packets.
interface network interface to listen
-h --help
""" %sys.argv[0]
def discover_neighbors (interface, timeout=100):
def on_cdp_packet (header, data):
ether_frame = ethernet.Ethernet (data)
cdp_packet = ether_frame.cdp
cdp_info = {}
for info in
cdp_info.update ({ info.type: })
addresses = [socket.inet_ntoa ( for x in cdp_info[cdp.CDP_ADDRESS]]
print "Hey, %s is at %s." %(cdp_info[cdp.CDP_DEVID], ", ".join (addresses))
pcap = pcapy.open_live (interface, 65535, 1, timeout)
pcap.setfilter ('ether[20:2] == 0x2000') # CDP filter
while True:
# this is more responsive to keyboard interrupts
pcap.dispatch (1, on_cdp_packet)
except KeyboardInterrupt, e:
except Exception, e:
print e
if __name__ == "__main__" :
options = docopt(__doc__)
discover_neighbors (options['<interface>'])
Copy link

nbd712 commented Jul 26, 2018

I am also having an error on "cdp_packet = ether_frame.cdp". Any advice?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment