python 获取区域网内所有以太网设备IP,并写入excel
时间: 2023-08-04 17:04:29 浏览: 107
yolov5s nnie.zip
可以使用Python的`scapy`库来实现获取局域网内设备IP的功能,并使用`openpyxl`库将结果写入Excel文件中。以下是一个简单的例子:
```python
from scapy.all import ARP, Ether, srp
from openpyxl import Workbook
# 构造ARP请求包
arp = ARP(pdst="192.168.1.0/24")
ether = Ether(dst="ff:ff:ff:ff:ff:ff")
packet = ether/arp
# 发送ARP请求并接收响应
result = srp(packet, timeout=3, verbose=0)[0]
# 解析响应,获取设备IP地址和MAC地址
devices = []
for sent, received in result:
devices.append({'ip': received.psrc, 'mac': received.hwsrc})
# 将结果写入Excel文件
wb = Workbook()
ws = wb.active
ws.append(['IP', 'MAC'])
for device in devices:
ws.append([device['ip'], device['mac']])
wb.save('devices.xlsx')
```
需要注意的是,这段代码需要以管理员权限运行才能正常工作。
阅读全文