def scan_hosts():
    # 获取本机IP地址
    local_ip = os.popen('ifconfig | grep 'inet ' | awk '{print $2}'').read().strip()
    # 获取本机MAC地址
    local_mac = os.popen('ifconfig | grep 'ether ' | awk '{print $2}'').read().strip()
    # 获取本机网段
    local_network = '.'.join(local_ip.split('.')[:-1]) + '.0/24'
    # 发送ARP请求包扫描主机
    ans, unans = srp(Ether(dst='ff:ff:ff:ff:ff:ff')/ARP(pdst=local_network), timeout=2, verbose=False)
    # 将扫描到的主机IP地址和MAC地址存入文件中
    with open('hosts.txt', 'w') as f:
        for send, recv in ans:
            f.write(recv.sprintf('%ARP.psrc% %Ether.src%
'))
    # 读取扫描到的主机列表
    with open('hosts.txt', 'r') as f:
        hosts = f.readlines()
    # 遍历主机列表,判断是否为本机
    for host in hosts:
        ip, mac = host.strip().split()
        if mac == local_mac:
            continue
        # 进行其他操作,如对主机进行端口扫描等

该脚本使用Python的scapy库进行ARP扫描,获取网络中所有主机的信息,并将其写入hosts.txt文件。

然后,脚本读取hosts.txt文件,遍历每个主机的信息,判断是否为本机,并根据需要进行其他操作,例如对主机进行端口扫描。

该脚本能够帮助您快速获取网络中所有主机的IP地址和MAC地址,并排除本机的信息,方便您进行进一步的操作。

Python 扫描网络主机并排除本机

原文地址: https://www.cveoy.top/t/topic/jpiG 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录