标签:
第一种是使用arping工具:
#!/usr/bin/env python import subprocess import sys import re def arping(ipaddress = "192.168.1.1"): p = subprocess.Popen("/usr/sbin/arping -c 2 %s" % ipaddress, shell = True, stdout = subprocess.PIPE) out = p.stdout.read() result = out.split() for item in result: if ":" in item: print item if __name__ == "__main__": if len(sys.argv) > 1: for ip in sys.argv[1:]: print "arping", ip arping(ip) else: arping()
这种方法依赖于特定平台的工具,整体就是使用了一个系统调用。
第二种是使用scapy,这种方法可以实现平台无关化
#!/usr/bin/env python from scapy import srp,Ether,ARP,conf import sys def arping(iprange="192.168.1.1/24"): conf.verb = 0 ans,unans = srp(Enter(dst="ff:ff:ff:ff:ff:ff")/ARP(pdst=iprange), timeout=2) collection = [] for snd, rcv in ans: result = rcv.sprintf(r"%ARP.psrc% %Ether.src%").split() collection.append(result) return collection if __name__ == "__main__": if len(sys.argv) > 1: for ip in sys.argv[1:]: print "arping", ip print arping(ip) else: print arping()
标签:
原文地址:http://www.cnblogs.com/x113/p/4658960.html