简体   繁体   English

从 ip 地址获取本地主机名,即 Windows 10 上的 192.168.1.x python 3

[英]get local hostname from ip adress i.e. 192.168.1.x python 3 on windows 10

currently using:- import socket当前使用:- 导入套接字

hostip='192.168.1.62'
getip=socket.getaddrinfo(hostip,port=22)

returns info but does not include Hostname.返回信息但不包括主机名。 seems to be a local DNS problem from other discussions.似乎是其他讨论中的本地 DNS 问题。 The local router finds all hostnames.本地路由器查找所有主机名。

Hostname is only maintained by windows or netbios, so from a command prompt it is virtually impossible to find.主机名仅由 windows 或 netbios 维护,因此从命令提示符几乎不可能找到它。 So I switched to searching for the ip address by referencing the Mac address.所以我切换到通过引用Mac地址来搜索IP地址。 The best solution so far is to use nmap and parse the results.迄今为止最好的解决方案是使用 nmap 并解析结果。 My code for ip addresses up to 192.268.1.99 is this我的 IP 地址高达 192.268.1.99 的代码是这样的

mac='0a:0b:0c:0d'
arpin=os.popen('nmap -sP -n 192.168.1.0/24',"r").read()
arl=arpin.split("\n")
fmac=[arl.index(i) for i in arl if mac in i]
fip=arl[fmac[0]-2][-12:]

fip gets the ip address of the device. fip 获取设备的 IP 地址。

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

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