Python中来自本地网络的IP地址/主机名列表 [英] List of IP addresses/hostnames from local network in Python
问题描述
如何使用Python轻松地从本地网络获取IP地址或主机名列表?
How can I get a list of the IP addresses or host names from a local network easily in Python?
最好是多平台的,但是它首先需要在Mac OS X上运行,然后才需要其他版本.
It would be best if it was multi-platform, but it needs to work on Mac OS X first, then others follow.
本地是指本地网络中的所有活动地址,例如192.168.xxx.xxx
.
By local I mean all active addresses within a local network, such as 192.168.xxx.xxx
.
因此,如果我的计算机(在本地网络中)的IP地址是192.168.1.1
,并且我有另外三台已连接的计算机,则希望它返回IP地址192.168.1.2
,192.168.1.3
,
So, if the IP address of my computer (within the local network) is 192.168.1.1
, and I have three other connected computers, I would want it to return the IP addresses 192.168.1.2
, 192.168.1.3
, 192.168.1.4
, and possibly their hostnames.
推荐答案
如果本地"是指同一网段,则必须执行以下步骤:
If by "local" you mean on the same network segment, then you have to perform the following steps:
- 确定您自己的IP地址
- 确定自己的网络掩码
- 确定网络范围
- 扫描所有地址(最低的地址是您的网络地址,最高的地址是您的广播地址).
- 使用DNS的反向查找来确定响应扫描的IP地址的主机名.
或者您也可以让Python在外部执行nmap并将结果通过管道传递回您的程序中.
Or you can just let Python execute nmap externally and pipe the results back into your program.
这篇关于Python中来自本地网络的IP地址/主机名列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!