Skip to content

Instantly share code, notes, and snippets.

@y0ug
Created September 21, 2014 08:35
Show Gist options
  • Save y0ug/8ddb697b5b8a243d0a36 to your computer and use it in GitHub Desktop.
Save y0ug/8ddb697b5b8a243d0a36 to your computer and use it in GitHub Desktop.
Scapy show CDP info
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import logging
import re
logging.getLogger("scapy").setLevel(logging.ERROR)
from scapy.all import *
def process_packets(pkt):
"""
Function for processing packets and printing information of CDP Packets
"""
p = pkt
# Check if the packet is a CDP Packet
if Dot3 in p and p.dst == '01:00:0c:cc:cc:cc':
print "\n*******************************"
print "Source MAC:", p.src
# Process each field in the packet message
for f in p[CDPv2_HDR].fields["msg"]:
# Check if the filed type is a known one
if f.type in _cdp_tlv_types:
# Process each field according to type
f_type = _cdp_tlv_types[f.type]
# Make sure we process each address in the message
if re.match(r"(Addresses|Management Address)", f_type):
for ip in f.fields["addr"]:
print f_type, ip.addr
elif f_type == "Software Version":
print f_type+":"
print "\t" + string.replace(f.val, "\n", "\n\t")
elif f_type == "Port ID":
print f_type, ":", f.iface
elif f_type == "Capabilities":
# Ugly but works :)
print f_type, ":", "".join(re.findall(r"cap\s*=(\S*)", str(f.show)))
elif re.match(r"Native VLAN|VoIP VLAN Reply",f_type):
print f_type, ":", f.vlan
elif f_type == "Duplex":
print f_type, ":", _cdp_duplex[f.duplex]
elif f_type == "IP Prefix":
print f_type, ":", f.defaultgw
elif f_type == "Power":
print f_type, ":", f.power, " mW"
# Fields not yet implemented in the current version of the
# contributed cdp module.
elif f_type == "Power Available":
# I know, this should provide the amount of power
print f_type, ": POE Enabled"
elif f_type == "Protocol Hello":
pass
else:
try:
# Make sure we do not have an empty value and print
if f.val is not '\0' and len(f.val) != 0: print f_type, ":", f.val
except Exception, e:
print "ERROR!!!!:", f_type
print e
pass
if __name__ == "__main__":
load_contrib("cdp")
sniff(iface = "eth1", prn = process_packets, store=0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment