ARP查询

  • 前言
  • 一、基础设置
  • 1.ARP查询(版本1)
  • 2.ARP查询(版本2)
  • 3.ARP查询(版本3)



前言

记录一些学习经验、遇到的问题


一、基础设置

1.ARP查询(版本1)

只查询单个IP
缺点1:查询单个IP是否存活(总时长254秒)

from scapy.all import *
from scapy.layers.l2 import ARP, Ether

### 查询单个IP的MAC地址
def arp1():
    ip = "192.168.1.1"  # 需要查询的IP

    arpPkt = Ether(dst="ff:ff:ff:ff:ff:ff") / ARP(pdst=ip)  
    # 报文格式:Ether广播,ARP查询IP
    # 发送arp请求,并获取响应结果。设置1s超时。
    res = srp1(arpPkt, timeout=1, verbose=1)  # srp1发送并接受,timeout 超时丢弃,verbose 显示详细内容

    if res:
        print(res.hwsrc)  # 输出查找IP的MAC地址
    else:
        print("不存在")

if __name__ == '__main__':
    arp1()

2.ARP查询(版本2)

通过while循环查询254个网段内IP
缺点1:每个报文超时1秒,所以耗时较长(总时长254秒)
缺点2:只能在通局域网、同vlan内查询

from scapy.all import *
from scapy.layers.l2 import ARP, Ether

###查询某个网段内IP对应的MAC地址
# (缺点1:每个报文超时1秒,所以耗时较长)
# (缺点2:只能在通局域网、同vlan内查询)
def arp2():
    network_ip = "192.168.1"  # 需要查询的IP网段
    t = 1
    while t < 255:
        ip = network_ip + '.' + str(t)

        arpPkt = Ether(dst="ff:ff:ff:ff:ff:ff") / ARP(pdst=ip)  # 报文格式:Ether广播,ARP查询IP
        # 发送arp请求,并获取响应结果。设置1s超时。
        res = srp1(arpPkt, timeout=1, verbose=0)  # srp1发送并接受,timeout 超时丢弃,verbose 显示详细内容
        if res:
            print("ip地址:" + ip + "  mac地址:" + res.hwsrc)  # 输出查找IP的MAC地址
        else:
            print("ip地址:" + ip + "不存在")
        t += 1
if __name__ == '__main__':
	arp2()

3.ARP查询(版本3)

通过创建5个进程,每个进程查询50个IP,并行查询
缺点1:每个报文超时1秒,所以耗时较长(五个进程并行查询,总时长54秒)
缺点2:只能在通局域网、同vlan内查询
缺点3:需要手动设置线程数量(5个)

from scapy.all import *
from scapy.layers.l2 import ARP, Ether
from PyQt5.QtCore import QThread  # 进程

###查询某个网段内IP对应的MAC地址(未完成)
# (缺点1:需要手动设置线程数量)
# (缺点2:只能在通局域网、同vlan内查询)
t1 = 1
network_ip = "192.168.1"  # 需要查询的IP网段

def arp3():
    global t1
    c = []

    c.append(qt1())
    c.append(qt2())
    c.append(qt3())
    c.append(qt4())
    c.append(qt5())

    c[0].start()
    c[1].start()
    c[2].start()
    c[3].start()
    c[4].start()

    while t1 <= 254:
        time.sleep(3)


class qt1(QThread):  ###创建进程
    def run(self):
        global t1,network_ip
        t = 1
        while t <= 50:
            ip = network_ip + '.' + str(t)
            print("1: " + str(ip))
            arpPkt = Ether(dst="ff:ff:ff:ff:ff:ff") / ARP(pdst=ip)  # 报文格式:Ether广播,ARP查询IP
            # 发送arp请求,并获取响应结果。设置1s超时。
            res = srp1(arpPkt, timeout=1, verbose=0)  # srp1发送并接受,timeout 超时丢弃,verbose 显示详细内容
            if res:
                print("ip地址:" + ip + "  mac地址:" + res.hwsrc)  # 输出查找IP的MAC地址
            else:
                print("ip地址:" + ip + "不存在")
            t += 1
            t1 +=1

class qt2(QThread):  ###创建进程
    def run(self):
        global t1
        t = 51
        while t <= 100:
            ip = network_ip + '.' + str(t)
            print("2: " + str(ip))
            arpPkt = Ether(dst="ff:ff:ff:ff:ff:ff") / ARP(pdst=ip)  # 报文格式:Ether广播,ARP查询IP
            # 发送arp请求,并获取响应结果。设置1s超时。
            res = srp1(arpPkt, timeout=1, verbose=0)  # srp1发送并接受,timeout 超时丢弃,verbose 显示详细内容
            if res:
                print("ip地址:" + ip + "  mac地址:" + res.hwsrc)  # 输出查找IP的MAC地址
            else:
                print("ip地址:" + ip + "不存在")
            t += 1
            t1 +=1

class qt3(QThread):  ###创建进程
    def run(self):
        global t1
        t = 100
        while t <= 150:
            ip = network_ip + '.' + str(t)
            print("3: " + str(ip))
            arpPkt = Ether(dst="ff:ff:ff:ff:ff:ff") / ARP(pdst=ip)  # 报文格式:Ether广播,ARP查询IP
            # 发送arp请求,并获取响应结果。设置1s超时。
            res = srp1(arpPkt, timeout=1, verbose=0)  # srp1发送并接受,timeout 超时丢弃,verbose 显示详细内容
            if res:
                print("ip地址:" + ip + "  mac地址:" + res.hwsrc)  # 输出查找IP的MAC地址
            else:
                print("ip地址:" + ip + "不存在")
            t += 1
            t1 +=1

class qt4(QThread):  ###创建进程
    def run(self):
        global t1
        t = 151
        while t <= 200:
            ip = network_ip + '.' + str(t)
            print("4: " + str(ip))
            arpPkt = Ether(dst="ff:ff:ff:ff:ff:ff") / ARP(pdst=ip)  # 报文格式:Ether广播,ARP查询IP
            # 发送arp请求,并获取响应结果。设置1s超时。
            res = srp1(arpPkt, timeout=1, verbose=0)  # srp1发送并接受,timeout 超时丢弃,verbose 显示详细内容
            if res:
                print("ip地址:" + ip + "  mac地址:" + res.hwsrc)  # 输出查找IP的MAC地址
            else:
                print("ip地址:" + ip + "不存在")
            t += 1
            t1 +=1

class qt5(QThread):  ###创建进程
    def run(self):
        global t1
        t = 201
        while t <= 254:
            ip = network_ip + '.' + str(t)
            print("5: " + str(ip))
            arpPkt = Ether(dst="ff:ff:ff:ff:ff:ff") / ARP(pdst=ip)  # 报文格式:Ether广播,ARP查询IP
            # 发送arp请求,并获取响应结果。设置1s超时。
            res = srp1(arpPkt, timeout=1, verbose=0)  # srp1发送并接受,timeout 超时丢弃,verbose 显示详细内容
            if res:
                print("ip地址:" + ip + "  mac地址:" + res.hwsrc)  # 输出查找IP的MAC地址
            else:
                print("ip地址:" + ip + "不存在")
            t += 1
            t1 +=1

if __name__ == '__main__':
	arp3()