简体   繁体   中英

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. 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. So I switched to searching for the ip address by referencing the Mac address. The best solution so far is to use nmap and parse the results. My code for ip addresses up to 192.268.1.99 is this

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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