How can I get a list of the IP addresses or host names from a local network easily in Python?
It would be best if it was multi-platform, but it needs to work on Mac OS X first, then others follow.
Edit: By local I mean all active addresses within a local network, such as 192.168.xxx.xxx
.
So, if the IP address of my computer (within the local network) is 192.168.1.1
, and I have three other connected computers, I would want it to return the IP addresses 192.168.1.2
, 192.168.1.3
, 192.168.1.4
, and possibly their hostnames.
Try:
If by "local" you mean on the same network segment, then you have to perform the following steps:
Or you can just let Python execute nmap externally and pipe the results back into your program.
Here is a small tool scanip that will help you to get all ip addresses and their corresponding mac addresses in the network (Works on Linux). This is the link for scanip (Ip and Mac scanner) written in python. https://pypi.python.org/pypi/scanip/1.0
You can also download it using pip install scanip on linux and to use it , create a test file in python and use it like this-
and run this program . All the ip and their corresponding mac addresses in the LAN will be shown in the terminal.
I have collected the following functionality from some other threads and it works for me in Ubuntu.
Update: The script is now located on github.
I wrote a small python script, that leverages scapy's
arping()
.If you know the names of your computers you can use:
Otherwise you will have to scan for all the IP addresses that follow the same mask as your local computer (IP1), as stated in another answer.