- #!/usr/bin/env python
- # arpings a network and outputs a table as a result
- import sys, re, subprocess, gzip
- from socket import inet_aton
- from struct import unpack
- if len(sys.argv) != 2:
- print "Usage: " + sys.argv[0] + " <interface>\n eg: " + sys.argv[0] + " eth0"
- sys.exit(1)
- iface = sys.argv[1]
- num_box = 0
- def calcCIDR(mask):
- mask = mask.split('.')
- bits = []
- for c in mask:
- bits.append(bin(int(c)))
- bits = ''.join(bits)
- cidr = 0
- for c in bits:
- if c == '1': cidr += 1
- return str(cidr)
- p1 = subprocess.Popen(["route", "-n"], stdout=subprocess.PIPE)
- p2 = subprocess.Popen(["grep", iface], stdin=p1.stdout, stdout=subprocess.PIPE)
- p1.stdout.close()
- output1 = p2.communicate()[0].split('\n')
- output2 = output1[0].split(' ')
- netinfo = filter(None, output2)
- if netinfo == "":
- print "No route found for " + iface
- sys.exit(1)
- #print netinfo
- #['192.168.2.0', '*', '255.255.255.0', 'U', '0', '0', '0', 'eth0']
- network = netinfo[0]+"/"+calcCIDR(netinfo[2])
- #print network
- from scapy.all import srp,Ether,ARP,conf
- conf.verb=0
- ans,unans=srp(Ether(dst="ff:ff:ff:ff:ff:ff")/ARP(pdst=network), timeout=2, iface=iface)
- for snd,rcv in ans:
- #print "test:" + Ether.src + "\n"
- mac = rcv.sprintf(r"%Ether.src%")
- ip = rcv.sprintf(r"%ARP.psrc%")
- info = ""
- for line in open("macdb.txt"):
- if mac[:8].upper().replace(":", "-") in line:
- info1 = re.sub("\n", "", line)
- info = re.sub(mac[:8].upper(), "" ,info1)
- num_box += 1
- print ip + " \t " + mac + " \t " + info
- print "total boxes: " + str(num_box)
scapy ARPing
Posted by Admin on Tue 22nd Feb 2011 06:17
raw | new post
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.