Python Forum
Find IP of a device on a local network
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Find IP of a device on a local network
#1
Hello!


I'm working on a project for which I need to get a devices IP, but I know the host name aka. ID.

What kind of module should I use for this, I've tried socket but I couldn't figure it out, any help?

Thanks!
Reply
#2
Read the docs: https://docs.python.org/3/library/socket.html
Reply
#3
Hello!
I don't know how to do it with socket module. Yet
In [1]: import netifaces

In [2]: netifaces.ifaddresses('wlp9s0')
Out[2]: 
{2: [{'addr': '192.168.100.3',
   'broadcast': '192.168.100.255',
   'netmask': '255.255.255.0'}],
 10: [{'addr': 'fe80::8558:4664:f9bb:a914%wlp9s0',
   'netmask': 'ffff:ffff:ffff:ffff::/64'}],
 17: [{'addr': '68:17:29:8b:ea:98', 'broadcast': 'ff:ff:ff:ff:ff:ff'}]}
May be socket.gethostbyname_ex('host_name') is what you want
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#4
From soshan on stackoverflow
>>> import socket

>>> def get_ips_for_host(host):
        try:
            ips = socket.gethostbyname_ex(host)
        except socket.gaierror:
            ips=[]
        return ips

>>> ips = get_ips_for_host('www.google.com')
>>> print(repr(ips))
('www.l.google.com', [], ['74.125.77.104', '74.125.77.147', '74.125.77.99'])
Reply
#5
(Jan-06-2017, 05:15 PM)mrburnzie Wrote: Hello!


I'm working on a project for which I need to get a devices IP, but I know the host name aka. ID.

What kind of module should I use for this, I've tried socket but I couldn't figure it out, any help?

Thanks!
The hostname is "local" to the device. In a pure IP network you would have to register that name/address pair on a DNS server, on in the /etc/hosts file (Windows/Systems32/Drivers/etc/hosts on a windows system). Once you have done that the usual TCP/IP name resolution APIs will work.

If all involved machines also implement the NETBIOS protocol, there is a way to retrieve the IP adress from the NETBIOS name (WinDNS?).

Otherwise if you are using a specific port, you can iterate all the local network address until you connect...
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Input network device connection info from data file edroche3rd 6 1,023 Oct-12-2023, 02:18 AM
Last Post: edroche3rd
  How to automate loop test check on Network device jpc230 1 584 Oct-09-2023, 09:54 PM
Last Post: Larz60+
  Failing to copy file from a network to a local drive tester_V 4 7,117 Jan-20-2021, 07:40 AM
Last Post: tester_V
  Reading UDP from external device without device software ikdemartijn 2 3,403 Dec-03-2019, 04:29 PM
Last Post: Larz60+
  Display device details i.e connected with more than one device shintonp 6 5,313 May-10-2017, 06:00 AM
Last Post: shintonp

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020