局域网扫描器(有待优化)-Python应用

import time,threading,socket,os,re,sys
from scapy.all import srp, Ether, ARP

threads = []
# 获取该IP计算机名、MAC地址
def GetHost(IPAddress):
    try:
        ans,unans = srp(Ether(dst="FF:FF:FF:FF:FF:FF")/ARP(pdst=IPAddress), timeout=2,verbose=False)
    except Exception as e:
        print("IP:%s     MAC地址获取失败!!!"%IPAddress)
    else:
        for send, rcv in ans:
            IPAddress = rcv.sprintf("%ARP.psrc%")
            MACAddress = rcv.sprintf("%Ether.src%")
            # 根据IP查询主机名
            try:
                (hostname,aliaslist,addresslist) = socket.gethostbyaddr(IPAddress)
                print("主机名:%s     IP地址:%s     MAC地址:%s"%(hostname, IPAddress, MACAddress))
            except Exception as e:
                print("主机名:%s     IP地址:%s     MAC地址:%s"%("获取失败", IPAddress, MACAddress))
                return

# 生成该段IP地址
def IPScan(IPPrefix):
    for i in  range(1,254):
        IPAddress = '%s.%s'%(IPPrefix,i)
        th = threading.Thread(target=GetHost,args=(IPAddress,))
        threads.append(th)

if __name__ == "__main__":
    print("开始时间:%s"%time.ctime())
    IPPrefix = input("请输入IP前三段(***.***.***):")
    IPScan(IPPrefix)
    for t in threads:
        t.start()
    # 主线程中等待所有子线程退出
    for t in threads:
        t.join()
    print("结束时间:%s"%time.ctime())
    os.system('pause') #按任意键继续

 

头像

作者:

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

Demon_运维笔记