python - 使用 Python 识别局域网上连接的设备路由器
问题描述
所以我有 4 台路由器和 1 台 PC,其中有 4 台设备连接到最近的路由器。所以我想做的是用python编写一个在PC(Windows)上运行的脚本,并且该PC连接到1个主路由器,该路由器连接到其他3个路由器。
1 台 PC -> 主路由器(启用 WiFi)-> 3 台其他路由器(启用 WiFi)
手环(支持 WiFi 的设备)将连接到 4 个路由器中最近的 WiFi 路由器。我想通过 Python 脚本检测手环连接到哪个路由器。我也可以将手镯的 IP 地址设为静态,但我希望使用 MAC 地址来识别它们。如果需要,我可以通过网页编辑路由器配置。
如果需要,请要求任何澄清。
希望尽快听到。
Edit_1:不要与我要求整个代码混淆,我正在寻找可用于此过程的正确功能以及我应该将路由器设置为哪些可能的配置。
Edit_2:下面是一个允许 ping IP 的示例代码。那么如何获取他们的 MAC 地址(功能)并 ping 到其他接入点?我可以更改路由器的属性。
import socket
from datetime import datetime
net = input("Enter the IP address: ") #e.g. is 192.168.18.1
net1 = net.split('.')
a = '.'
net2 = net1[0] + a + net1[1] + a + net1[2] + a
st1 = int(input("Enter the Starting Number: ")) #e.g. 1
en1 = int(input("Enter the Last Number: ")) #e.g. 10
en1 = en1 + 1
t1 = datetime.now()
def scan(addr):
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
socket.setdefaulttimeout(1)
result = s.connect_ex((addr,135))
if result == 0:
return 1
else :
return 0
def run1():
for ip in range(st1,en1):
addr = net2 + str(ip)
if (scan(addr)):
print (addr , "is live")
run1()
t2 = datetime.now()
total = t2 - t1
print ("Scanning completed in: " , total)
Edit_3:这也会搜索 MAC 地址。
from scapy.all import ARP, Ether, srp
target_ip = "192.168.18.1/24"
# IP Address for the destination
# create ARP packet
arp = ARP(pdst=target_ip)
# create the Ether broadcast packet
# ff:ff:ff:ff:ff:ff MAC address indicates broadcasting
ether = Ether(dst="ff:ff:ff:ff:ff:ff")
# stack them
packet = ether/arp
result = srp(packet, timeout=3, verbose=0)[0]
# a list of clients, we will fill this in the upcoming loop
clients = []
for sent, received in result:
# for each response, append ip and mac address to `clients` list
clients.append({'ip': received.psrc, 'mac': received.hwsrc})
# print clients
print("Available devices in the network:")
print("IP" + " "*18+"MAC")
for client in clients:
print("{:16} {}".format(client['ip'], client['mac']))
if (client['mac'] == "f4:f5:e8:37:67:92"): #Testing mac address filter
print("Success")
解决方案
推荐阅读
- python - 在python中比较多维分布的测试
- reactjs - 如何在 ag-grid(react)树数据中的下拉箭头旁边显示孩子的数量?
- typescript - 打字稿 - 从参数元组中删除最后一个元素,取消最后一个参数
- angular - 创建“任何”类型的扩展方法。错误:“任何”仅指一种类型,但在此处用作值
- c# - MaterialPropertyBlock 在运行时不会改变颜色?
- android - 如何在 Android 中创建自定义画笔
- javascript - Gridstack - 如果我在重新初始化之前破坏网格,则稍后会失败
- azure - 如何永久清除在启用软删除的情况下创建的整个 azure keyvault?
- python - 这个 Python 代码中的 [:5] 和 [5] 有什么区别?
- docker - 如何创建在 docker 中运行的自由式 jenkins 作业