我有一些代码可以扫描无线数据包并显示每个数据包的 MAC 地址。我想要做的是拥有一个 mac 地址的文本文件,并且当无线扫描中拾取文件中的某个地址时,代码会通过消息提醒我。我想不出实现此目的的方法,这是无线扫描的代码,下面是文本文件的示例。
import sys
from scapy.all import *
devices = set()
def PacketHandler(pkt):
if pkt.haslayer(Dot11):
dot11_layer = pkt.getlayer(Dot11)
if dot11_layer.addr2 and (dot11_layer.addr2 not in devices):
devices.add(dot11_layer.addr2)
print dot11_layer.addr2
sniff(iface = sys.argv[1], count = int(sys.argv[2]), prn = PacketHandler)
这是文本文件的示例。
00:11:22:33:44:55
AA:BB:CC:DD:EE:FF
创建一个从 .txt 读取并将每一行(与 MAC 地址匹配)存储在列表中的函数。
def getListMac() -> list: # you can put the path for your .txt file as argument
with open('MAClist.txt', 'r+') as file:
res = [x.rstrip('\n') for x in file.readlines()]
return res
然后检查您的 packetHandler 函数,如果 mac 在此列表中。 这里你有两个选择:
MACLIST = getListMac()
...
# in your PacketHandler function
if mac in MACLIST:
print("mac found!") #or whatever your want to do
# in your PacketHandler function:
if mac in getListMac():
print("mac found!") # or whatever your want to do
最后,我将建议您使用真正的 DBMS 来结束这篇文章,这比读取 txt 文件要高效得多。 ;)
编辑
回答您的评论:
修改 getListMac 函数以便将信息存储在字典中。
这里是一个示例,假设您使用“-”作为 MAC - 时间 - 用户名之间的分隔符
def getListMac() -> dict: # you can put the path for your .txt file as argument
with open('MAClist.txt', 'r+') as file:
res = {x.rstrip('\n').split(" - ")[0]: x.rstrip('\n').split(" - ")[2] for x in file.readlines()}
return res
像这样访问字典中的数据:
if MAC in MACLIST:
print(f"MAC found -> {MAC}, Username -> {MACLIST[MAC]}")