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

让人想犯罪 __ 提交于 2021-01-29 22:19:24

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!