Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List of IP addresses/hostnames from local network in Python

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.

like image 611
Josh Hunt Avatar asked Oct 16 '08 02:10

Josh Hunt


2 Answers

If by "local" you mean on the same network segment, then you have to perform the following steps:

  1. Determine your own IP address
  2. Determine your own netmask
  3. Determine the network range
  4. Scan all the addresses (except the lowest, which is your network address and the highest, which is your broadcast address).
  5. Use your DNS's reverse lookup to determine the hostname for IP addresses which respond to your scan.

Or you can just let Python execute nmap externally and pipe the results back into your program.

like image 74
Steve Moyer Avatar answered Sep 29 '22 02:09

Steve Moyer


Update: The script is now located on github.

I wrote a small python script, that leverages scapy's arping().

like image 27
Benedikt Waldvogel Avatar answered Sep 29 '22 01:09

Benedikt Waldvogel