qianqianling 发表于 2018-8-9 09:22:23

python 使用scapy创建arping脚本

  以下程序均来自《Python.UNIX和Linux系统管理指南》
  据说scapy是一个很厉害的东西
  使用的scapy版本为2.1.0,注意导入包的方式与原书略有差别
scapy_arping.py  
#!/usr/bin/env/python
  
from scapy.all import srp,Ether,ARP,conf
  
import sys
  
def arping(iprange="192.168.137.0/24"):
  
"""Arping function takes IP Address or Network, returns nested mac/ip list"""
  
conf.verb=0
  
ans, unans=srp(Ether(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:
  
print "arping", ip
  
print arping(ip)
  
else:
  
print arping()
  运行结果:
  # python scapy_arping.py
  [['192.168.137.1', '00:50:56:c0:00:08']]
  注意事项:
  在运行过程中可能会有ipv6的warning
  WARNING: No route found for IPv6 destination :: (no default route?)
  解决方法:修改scapy安装目录下的all.py
  注释掉下面几行
  #if conf.ipv6_enabled:
  #    from utils6 import *
  #    from route6 import *
  重新python setup.py install即可
页: [1]
查看完整版本: python 使用scapy创建arping脚本