环境:Python 3.x + scapy
def ip_mac_scanner(hosts: str, local_mac: str, detail: bool = False):
"""
网段IP&Mac ARP协议扫描器
:param hosts: 网段 e.g.‘*.*.*.*/*’
:param local_mac: 本地MAC地址,e.g.‘**-**-**-**-**-**’
:param detail: 是否显示详细信息
:return: dict { IP: MAC, .... }
"""
from scapy.layers.l2 import Ether, ARP
from scapy.sendrecv import srp
import warnings
if detail:
print('scanning %s by ARP...' % hosts)
packet = Ether(dst="ff:ff:ff:ff:ff:ff", src=local_mac)/ARP(pdst=hosts)
if detail:
_Answer, _unAnswer = srp(packet, timeout=2, verbose=3)
else:
_Answer, _unAnswer = srp(packet, timeout=2, verbose=0)
if detail:
print("%d host(s) found:" % len(_Answer))
result = {}
for Send, Receive in _Answer:
_IP = Receive[ARP].psrc
_M