pastebin - collaborative debugging tool
kpaste.net RSS


scapy ARPing
Posted by Admin on Tue 22nd Feb 2011 06:17
raw | new post

  1. #!/usr/bin/env python
  2. # arpings a network and outputs a table as a result
  3.  
  4. import sys, re, subprocess, gzip
  5. from socket import inet_aton
  6. from struct import unpack
  7.  
  8. if len(sys.argv) != 2:
  9.     print "Usage: " + sys.argv[0] + " <interface>\n eg: " + sys.argv[0] + " eth0"
  10.     sys.exit(1)
  11.  
  12. iface = sys.argv[1]
  13.  
  14. num_box = 0
  15.  
  16. def calcCIDR(mask):
  17.     mask = mask.split('.')
  18.     bits = []
  19.     for c in mask:
  20.        bits.append(bin(int(c)))
  21.     bits = ''.join(bits)
  22.     cidr = 0
  23.     for c in bits:
  24.         if c == '1': cidr += 1
  25.     return str(cidr)
  26.  
  27. p1 = subprocess.Popen(["route", "-n"], stdout=subprocess.PIPE)
  28. p2 = subprocess.Popen(["grep", iface], stdin=p1.stdout, stdout=subprocess.PIPE)
  29. p1.stdout.close()
  30. output1 = p2.communicate()[0].split('\n')
  31. output2 = output1[0].split(' ')
  32. netinfo = filter(None, output2)
  33. if netinfo == "":
  34.     print "No route found for " + iface
  35.     sys.exit(1)
  36.  
  37. #print netinfo
  38. #['192.168.2.0', '*', '255.255.255.0', 'U', '0', '0', '0', 'eth0']
  39.  
  40. network = netinfo[0]+"/"+calcCIDR(netinfo[2])
  41. #print network
  42.  
  43. from scapy.all import srp,Ether,ARP,conf
  44. conf.verb=0
  45. ans,unans=srp(Ether(dst="ff:ff:ff:ff:ff:ff")/ARP(pdst=network), timeout=2, iface=iface)
  46.  
  47. for snd,rcv in ans:
  48.         #print "test:" + Ether.src + "\n"
  49.         mac = rcv.sprintf(r"%Ether.src%")
  50.         ip = rcv.sprintf(r"%ARP.psrc%")
  51.         info = ""
  52.         for line in open("macdb.txt"):
  53.                 if mac[:8].upper().replace(":", "-") in line:
  54.                         info1 = re.sub("\n", "", line)
  55.                         info = re.sub(mac[:8].upper(), "" ,info1)
  56.         num_box += 1
  57.         print ip + " \t " + mac + " \t " + info
  58. print "total boxes: " + str(num_box)

Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:

To highlight particular lines, prefix each line with {%HIGHLIGHT}




All content is user-submitted.
The administrators of this site (kpaste.net) are not responsible for their content.
Abuse reports should be emailed to us at