简体   繁体   English

在 Windows 中使用 python 3 枚举连接到我的路由器的设备

[英]Enumerate devices connected to my router using python 3 in Windows

I'm trying to write a script to check how many devices are connected to the internet.我正在尝试编写一个脚本来检查有多少设备连接到互联网。 This is part of a bigger project to prove to my ISP that my internet sucks.这是一个更大项目的一部分,目的是向我的 ISP 证明我的互联网很糟糕。 I am performing a speed test every minute, and logging it to a text file.我每分钟执行一次速度测试,并将其记录到一个文本文件中。 I would like to log, at the same time, how many devices are connected, just to make sure that the problem isn't that the internet only sucks once a certain number of devices are connected.我想同时记录连接了多少设备,只是为了确保问题不是只有在连接了一定数量的设备后互联网才会变得糟糕。 There aren't multiple people streaming on this network, so I know that isn't the problem, but if it is a devices problem, that might be on me to upgrade my internet.这个网络上没有多人流式传输,所以我知道这不是问题所在,但如果是设备问题,我可能需要升级我的互联网。 I tried using the modules neighbourhood and lanscan , but I can't get them to work on my machine.我尝试使用模块neighbourhoodlanscan ,但我无法让它们在我的机器上工作。

With lanscan I tried lanscan.lanscan.networks() and get the error message使用lanscan我尝试lanscan.lanscan.networks()并收到错误消息

"module 'lanscan' has no attribute 'lanscan'" “模块‘lanscan’没有属性‘lanscan’”

My python IDE suggests that these modules should exist with this structure.我的 python IDE 建议这些模块应该以这种结构存在。 lanscan.networks() give the same error except that it says "'networks' doesn't exist". lanscan.networks()给出相同的错误,除了它说“‘网络’不存在”。 Also, neighbourhood uses functions that don't work in windows, like os.geteuid() , so I don't think it is compatible with windows.此外, neighbourhood使用的函数在 windows 中不起作用,例如os.geteuid() ,所以我认为它与 windows 不兼容。

Is there a way I can find which of my devices are currently connected to my internet network?有什么方法可以找到我的哪些设备当前连接到我的互联网网络? Actually, All I really need is the number of devices.实际上,我真正需要的只是设备的数量。 I know that if I connect the IP address of my router/modem I can see the connected devices as names, and their IP addresses, so I should be able to find this information somehow, I feel.我知道如果我连接我的路由器/调制解调器的 IP 地址,我可以看到连接的设备的名称和它们的 IP 地址,所以我觉得我应该能够以某种方式找到这些信息。

Neighborhood Lanscan 邻里扫描

I found one answer, though it doesn't appear to be 100% accurate.我找到了一个答案,尽管它似乎不是 100% 准确。 I notices that all the devices that connect to my router get straightforward names.我注意到所有连接到我的路由器的设备都有直截了当的名称。 My ip address is 192.168.0.1, so my devices are 192.168.0.1 0 , 192.168.0.1 1 , 192.168.0.1 2 , etc. Therefore I just ping the first 10 devices.我的 IP 地址是 192.168.0.1,所以我的设备是 192.168.0.1 0 、 192.168.0.1 1 、 192.168.0.1 2等。因此我只 ping 前 10 个设备。 I don't necessarily trust the response alone though.不过,我不一定只相信回应。 Once pinged I run arpa -a through the windows system with subprocess. ping 后,我通过带有子进程的 Windows 系统运行arpa -a

import subprocess

#this for loop depends on ho wlong you are willing to wait. I am
for i in range(10):   #look for up to 10 devices
    command=['ping', '-n', '1', '192.168.0.1'+str(i)]   #icrement the device names
    subprocess.call(command)   #ping the devices to update data before  "arp -a'

arpa = subprocess.check_output(("arp", "-a")).decode("ascii") #call 'arp -a' and get results

#I count lines that contain 192.1868, but not ['192.168.0.1 ','192.168.0.255'] 
#because those are the router and broadcast gateway. Note that the machine 
#you are running the code from will get counted because it will be in the 
#first line "Interface: 192.168.0.10 --- 0x4" in my case
n_devices=len([x for x in arpa.split('\n') if '192.168' in x and  all(y not in x for y in ['192.168.0.1 ','192.168.0.255']) ])

A second way is this, which is slower.第二种方法是这样,速度较慢。 This checks all ips from 0 to 255. I switched to an xfinity router, and found that they use pretty random numbers when assigning dynamic ips, unlike the motorola, which simply uses serial numbers starting from 192.168.0.10 (in my model).这会检查从 0 到 255 的所有 ip。我切换到 xfinity 路由器,发现它们在分配动态 ip 时使用相当随机的数字,这与摩托罗拉不同,摩托罗拉仅使用从 192.168.0.10 开始的序列号(在我的模型中)。 This answer is more general, then.那么这个答案更笼统。 I look at all 255 possibilities, but I limit the response time to 100ms (with teh paramter '-w' and '100', so it doesn't take forever. Should take about 25 seconds to ping everything, but if found it takes more like a minute.我查看了所有 255 种可能性,但我将响应时间限制为 100 毫秒(使用参数“-w”和“100”,所以它不会永远花费。应该花费大约 25 秒来 ping 一切,但如果发现它需要更像是一分钟。

for i in range(255):
    command=['ping', '-n', '1','-w','100', '10.0.0.'+str(i)]
    subprocess.call(command)

arpa = subprocess.check_output(("arp", "-a")).decode("ascii")
n_devices=len([x for x in arpa.split('\n') if '10.0.0.' in x and  
    all(y not in x for y in ['10.0.0.1 ','10.0.0.255']) ])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM