Get IPs of all devices connected to local network (based on hostname) with python

Get IPs of all devices connected to local network (based on hostname) with python



this is definetly not duplicate.



I am doing an app and I need to find IPs of my devices. I know their name, and through their name I need to get their IP. Linux should be easy but I need cross platform attitude and so therefore I use python.



I already know solution:


import socket
socket.gethostbyname('pc_name')



If I know solution, why I am asking? Well give me moment, because I am not getting stable results.



Situation:
I need to find IPs of several raspberry pi's that has same hostname - lets say:


Raspberry pi device 1: Hostname: 'MyPi', Local IP: 10.0.0.33

Raspberry pi device 2: Hostname: 'MyPi', Local IP: 10.0.0.34

Raspberry pi device 3: Hostname: 'MyPi', Local IP: 10.0.0.35



So according to socket documentation(If I read it correctly),


socket.gethostbyname('MyPi')



returns only one IP, HOWEVER


socket.gethostbyname_ex('MyPi')



should return all IPs for the particular hostname.



I simulated the situation at home and the results were very unsatisfying.
I called my raspberry pi as my Android phone: 'Galaxy-J5-2016'. To make sure I tried to find them by address:


>>> socket.gethostbyaddr('10.0.0.33')
('Galaxy-J5-2016', , ['10.0.0.33'])
>>> socket.gethostbyaddr('10.0.0.34')
('Galaxy-J5-2016', , ['10.0.0.34'])



Good , they exist. When I tested it before writing here, I even got one result if I searched


socket.gethostbyname('Galaxy-J5-2016')



BUT now I dont find anything even if I try


socket.gethostbyname_ex('Galaxy-J5-2016')



Both above commands simply wait without response and then respond with this:


Traceback (most recent call last):
File "<stdin>", line 1, in <module>
socket.gaierror: [Errno 11001] getaddrinfo failed



Another phenomena occured: When I try lastly mentioned command on my work laptop(Even though my work PC doesnt share hostname with any other computer),


>>> socket.gethostbyname_ex('WORK_PC.DOM1.DOM2')
('WORK_PC.DOM1.DOM2', , ['172.xx.xx.1', '10.0.xx.1', '172.xx.xx.241',
'192.xx.xx.1', '192.xx.xx.1', **'10.0.0.41'**])



Interestingly 10.0.0.41 is local ip address that my WORK PC has right now at home (The rest seems are from network at my job).



In other words: I have two machines with same Hostname and I get zero or only one IP adress and my work PC is just one MACHINE and i get several IPs of the same machine and these IPs so to say are not even from the actual local network)



PS (In case you will advice me 'arp -a'): general command 'arp -a' is useless as it shows all local IPs that were in recent contact with the command-machine. So I need to ping the particular devices to see them listed in arp -a command.



PS2: Also I dont want any solution that requires going through all IPs. This solution is very slow as pinging IP that doesnt exist will result in several seconds timeout. 255 IP + timeout is not good.



Honestly, If there exist python command to immediatly list all devices connected to Local Network, its enough... But I didnt find anything like this.



Am I doing something wrong? Can you direct me?



Thanks in Advance





It is not a requirement for devices connected to a network to report / return their names based on an external query. Your most likely resolution involves the router or however the IP addresses are assigned.
– dawg
Aug 25 at 15:54





When my raspberry pi was called 'MyPi' and was the only one in network I could find its ip by its name... when I named it like some other device, the problem started
– Martin
Aug 25 at 17:58









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)