问题
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.
回答1:
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.
来源:https://stackoverflow.com/questions/60571731/get-local-hostname-from-ip-adress-i-e-192-168-1-x-python-3-on-windows-10