Python - Get computer's IP address and host name on network running same application

40,059

Solution 1

If you want to get the IP address of the host on which the python script is running, then the answer provided by user2096338 is the way to go.

In order to discover all the computers on a network within a LAN you can use scapy. https://github.com/bwaldvogel/neighbourhood/blob/master/neighbourhood.py is a link that I found while browsing a similar SO question; which discovers all computers within a network (LAN).

If you want to ensure that the above script returns only those computers that are running your server - then there are multiple ways to do so, listed below in order of portablility

  1. Whenever you install your server on a host, then register itself into a central database which can then be queried by a new instance of your service to identify all computers where the peer servers are running
  2. Use WMI (since you are on Windows) to query all processes running on the peer system anc check if your server process is one amongst them.
  3. Your server will keep a probing port open and a new server installation will ping to the server on this probing port for each of the hosts in the network. If it gets a response, then this computer is running the server process.

In my opinion, method 1 is the safest and portable, since it prevents the opening up of unnecessary ports( like method 3) which are security holes (depending on whose context your python script runs). It also does not depend on the availability of the WMI service on the remote hosts ( some systems may have WMI disabled).

Hope this helps

Solution 2

For the hostname and ip of the localhost you could use the socket module and gethostname, and gethostbyname methods:

import socket

hostname = socket.gethostname()
IP = socket.gethostbyname(hostname)
Share:
40,059
RPDeshaies
Author by

RPDeshaies

Updated on July 17, 2022

Comments

  • RPDeshaies
    RPDeshaies almost 2 years

    Just to be clear: I just started Python 2 weeks ago but I'm a C#, ASP, PHP, JavaScript developer.

    I just started a new project with Python and PyQt and I want my project to be a server that will be able to communicate with other instance of this server on other computers.

    So, I need to get computers IP address and host name.

    First, I thought about using the MSDOS "net view" command but this was before trying this command at my job and seeing that this could take around ~15s (which I think is too slow).

    I had this idea from: https://stackoverflow.com/a/15497083/1598891

    Also, the "net view" command can return computers that are no longer on the network and cause exception when trying to get their IP addresses by host name (which also slows down the process each time a computer cannot be accessed).

    My Question: Is there a way to get all computers IP and Host Name on the Local network without passing by net view on Windows because net view could be a slow operation?

    Also, is there a way to only get computers that are running my application? (so it would be faster)

    Thanks in advance

  • RPDeshaies
    RPDeshaies over 10 years
    This doesn't answer my question, my question is : If there a way to get all computers IP and Host Name on the network without passing by net view on Windows because net view could be a slow operation
  • Peter Party Bus
    Peter Party Bus over 10 years
    I apologize. So you are looking for all the boxes on your network that are running your app, and the check has to be done in around under ~15 seconds?
  • RPDeshaies
    RPDeshaies over 10 years
    No problem, Yes this is what I would like to do. I would like this process to be the fastest as possible
  • RPDeshaies
    RPDeshaies over 10 years
    It helps very well. Does neighbourhood.py works on Windows ? Also, I think, since this is only a little application and I have no intention to use a database for it, that I'll probably use the method 3. But first I'll read more about method 2. Thanks for you help. I'll mark your answer as the "Accepted Answer" once I'll have tested scapy and neighbourhood this evening.
  • Prahalad Deshpande
    Prahalad Deshpande over 10 years
    Sure thing :). Thanks for the feedback. Yes and as per my knowledge this neighbourhood.py must work on Windows too since it uses scapy which is also available for Windows.
  • Peter Party Bus
    Peter Party Bus over 10 years
    Well it depends how you are detecting your app, if you have an open socket for example you can just for loop with the socket module and check for open ports on the network. Here are some examples: stackoverflow.com/questions/166506/… Other than that I think Prahalad Deshpandes post has the correct idea as far as best practices go. Goodluck. Cheers.
  • RPDeshaies
    RPDeshaies over 10 years
    I've added Scapy to my project and neightbourhood.py too, but when i'm running it, I'm getting the following error : ImportError: cannot import name debug For the line : import scapy.layers.l2 Also, Eclipse don't seems to recognize the following line (in neihbourhood, line 51) for network, netmask, _, interface, address in scapy.config.conf.route.routes: (undefined variable for import route)
  • Prahalad Deshpande
    Prahalad Deshpande over 10 years
    And you are sure that scapy has been added to your PYTHONPATH?
  • RPDeshaies
    RPDeshaies over 10 years
    Since I'm new to Python, I did not know that I needed to install the module. I thought that I only needed to import the code into my project. I'll try to install it (with cmd : python setup.py install ?) this evening. Sorry for bothering you for such a basic problem. Do I need to install every python module in Python that I want to use in my projects ?
  • Prahalad Deshpande
    Prahalad Deshpande over 10 years
    Yes, you can use python tools like PIP or easy_install that will manage package installations for you. Refer docs.python.org/2/tutorial/modules.html for details
  • RPDeshaies
    RPDeshaies over 10 years
    Even if I installed the scapy software I still have the same two errors. Do you have any idea of what caused that ? If no, do you have another way, instead of neighbourhood, to list all computer hostname and ip adress from scapy ?
  • Prahalad Deshpande
    Prahalad Deshpande over 10 years
    @Tareck117 I am sure, this is a simple configuration problem related to the PYTHONPATH. Basically PYTHONPATH specifies the path locations where the python interpreter will look for modules while bootstrapping your script. Using what tool did you install the scapy software?
  • RPDeshaies
    RPDeshaies over 10 years
    Directly with the windows command prompt using the setup.py install from the scapy folder
  • vallentin
    vallentin about 8 years
    Isn't Scapy for anything but local addresses?