Skip to content

Instantly share code, notes, and snippets.

Created July 12, 2013 05:02
Show Gist options
  • Save trhura/5981683 to your computer and use it in GitHub Desktop.
Save trhura/5981683 to your computer and use it in GitHub Desktop.
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

thats very good, thank you

Copy link

I'm trying to get this working and I get an error on the line: cdp_packet = ether_frame.cdp

Did this code every work?

Copy link

opnine commented Dec 27, 2017

is this working, ? how to I execute it?

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