Skip to content

Instantly share code, notes, and snippets.

@trhura
Created July 12, 2013 05:02
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • 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.
Arguments:
interface network interface to listen
Options:
-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_packet.data:
cdp_info.update ({ info.type: info.data })
addresses = [socket.inet_ntoa (x.data) for x in cdp_info[cdp.CDP_ADDRESS]]
print "Hey, %s is at %s." %(cdp_info[cdp.CDP_DEVID], ", ".join (addresses))
try:
pcap = pcapy.open_live (interface, 65535, 1, timeout)
pcap.setfilter ('ether[20:2] == 0x2000') # CDP filter
try:
while True:
# this is more responsive to keyboard interrupts
pcap.dispatch (1, on_cdp_packet)
except KeyboardInterrupt, e:
pass
except Exception, e:
print e
if __name__ == "__main__" :
options = docopt(__doc__)
discover_neighbors (options['<interface>'])
@chrisbog
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?

@opnine
Copy link

opnine commented Dec 27, 2017

is this working, ? how to I execute it?

@nbd712
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