使用scapy第三方库,编写一个可以对一个网段进行ARP扫描,获得该往段内活跃主机的IP地址和MAC地址。
代码示例如下:
#!/usr/bin/env python
# _*_ coding=utf-8 _*_
from scapy.all import *
import sys,getopt
def usage():
print "Usage: sudo ./ArpScanner.py "
def main(argv):
try:
opts, args = getopt.getopt(argv, "")
except getopt.GetoptError:
usage()
sys.exit(2)
for ipFix in range(1,254):
ip = "192.168.1."+str(ipFix)
arpPkt = Ether(dst="ff:ff:ff:ff:ff:ff")/ARP(pdst=ip, hwdst="ff:ff:ff:ff:ff:ff")
res = srp1(arpPkt, timeout=1, verbose=0)
if res:
print "IP: " + res.psrc + " MAC: " + res.hwsrc
if __name__ == "__main__":
main(sys.argv[1:])
原文地址:http://blog.csdn.net/jeanphorn/article/details/46408127