在网络扫描仪中过滤MAC地址的打印结果

问题描述 投票:1回答:1

我是一个初学者,但我正在尝试制作一个网络扫描实用程序,该程序可以过滤特定需求的MAC地址;我工作的公司有网络连接的设备,这些设备根据序列号分配了MAC地址。 MAC地址的最后6位数字是设备的序列号。 MAC地址的第4个字段是一小段常量,它们表示设备的型号,这是我需要知道的。

我正在努力寻找一种方法,以“过滤”从扫描中检索到的MAC地址,并根据MAC地址中第4个字段的变量对其进行标记。有了大量的阅读和帮助,到目前为止,我已经做到了:

import scapy.all as scapy
from scapy.all import ARP, Ether, srp

target_ip = "192.168.1.1/24"
arp = ARP (pdst=target_ip)
ether = Ether(dst="ff:ff:ff:ff:ff:ff")
packet = ether/arp

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

clients= []

for sent, received in result:
'clients' list
    clients.append({'ip': received.psrc, 'mac': received.hwsrc})

print("Available devices in the network:")
print("IP" + " "*18+"MAC")

for client in clients:
    print("{:16}    {}".format(client['ip'], client['mac']))

[我想知道是否可以创建一个数组或第4个字段数字的文本文件,并从那里读取脚本,以及最后如何将其实现到打印稿中。

python networking scapy
1个回答
0
投票

使用Scapy内置函数比重写它们更有意义。在这种情况下,请使用arping。为了节省时间(如此处),请阅读the manual

这将把第四个八位位组打印到文件中:

from scapy.all import arping

local_devices = arping("192.168.1.0/24")
local_macs = [device[1].src for device in local_devices[0]]
fourth_octets = [mac[9:11] for mac in local_macs]
with open("fourth_octets.txt", "w") as f:
     f.write("\n".join(fourth_octet))

Scapy口译员的解释

>>> # Get local devices. Four have been found, and 252 provided no answer.
>>> local_devices = arping("192.168.128.0/24")
>>> local_devices
(<ARPing: TCP:0 UDP:0 ICMP:0 Other:4>,
 <Unanswered: TCP:0 UDP:0 ICMP:0 Other:252>)

>>> # Get the mac addrs of each of the ARP responses with a list comprehension
>>> local_macs = [device[1].src for device in local_devices[0]]
>>> local_macs
['e0:55:3d:4d:e4:58',
 '00:18:0a:27:26:ee',
 'e0:55:3d:d2:0a:12',
 '38:53:9c:08:b9:9f']

>>> # Get the fourth octet by string position in the macs
>>> fourth_octets = [mac[9:11] for mac in local_macs]
>>> fourth_octets
['4d', '27', 'd2', '08']

>>> # Write to the file with the fourth octets, each on a line
>>> with open("fourth_octets.txt", "w") as f:
     f.write("\n".join(fourth_octets))

>>> # Verify that the text file looks as expected
>>> with open("fourth_octets.txt") as f:
     print(f.read())

4d
27
d2
08
© www.soinside.com 2019 - 2024. All rights reserved.