获取局域网内IP地址和MAC地址

from scapy.all import ARP, Ether, srp

def scan_devices(ip_range):
    arp = ARP(pdst=ip_range)
    ether = Ether(dst="ff:ff:ff:ff:ff:ff")
    packet = ether / arp

    result = srp(packet, timeout=3, verbose=0)[0]

    devices = []
    for sent, received in result:
        devices.append({'ip': received.psrc, 'mac': received.hwsrc})

    return devices

# 你可以指定局域网内的IP地址范围,例如'192.168.1.1/24'
ip_range = '10.12.135.65/24'
devices = scan_devices(ip_range)

for device in devices:
    print(f"IP: {device['ip']}, MAC: {device['mac']}")


你可能感兴趣的:(计算机网络,tcp/ip,网络协议)