-1

I am writing script in python, where I need to have IP addresses of active station in by subnet, what is 10.1.0.0/8. Is there some elegant way, how to get list of these stations from subnet, which will be fast?

Is there any chance to get this list from DNS server?

lkip0209
  • 15
  • 1
  • 7
  • What do you mean by "station in subnet"? – jazgot Oct 14 '19 at 06:11
  • any computer/server connected to network – lkip0209 Oct 14 '19 at 06:13
  • 2
    Enumerate all the IPs in your subnet and ping them to figure out if that IP has been allocated to a station or not. – rdas Oct 14 '19 at 06:16
  • That is one of the option, I have modified my question, if it is possible to get it from DNS server? – lkip0209 Oct 14 '19 at 06:40
  • 1
    I think, there is no faster solution than the one of @rdas. That's why network scanners take so long. – bb1950328 Oct 14 '19 at 07:04
  • If you can query your DHCP server then you might be able to get a list of all the allocated addresses – rdas Oct 14 '19 at 07:05
  • @rdas, that may work, but what is when someone uses a static ip? The dhcp server only knows about hosts which have a dynamic ip. – bb1950328 Oct 14 '19 at 07:08
  • Usually DHCP servers will be configured with the static IP setting of a host as well, since the server will need to know that this particular IP cannot be allocated to anybody else. – rdas Oct 14 '19 at 07:10
  • @rdas usually yes, but every client can just use a whichever ip address he wants without asking the dns server. Thats how I hacked my children restrictions on my parents router – bb1950328 Oct 14 '19 at 14:25

1 Answers1

1

I don't think that you can query a dns server for online hosts, as stated in my comments under the question. But you can use ping. The following code generates all possible IP Addresses in the specified range.

You then only need to filter them by sending a ping. The ping option -c 1 only sends one ping, the -w 2 is a hard timeout after 2 seconds. On Windows, you have to use -n 1

Note that there is a huge performance potential (you can send only one ping, run the pings in parallel, send the pings with a library etc)

(IP-Address conversion from https://stackoverflow.com/a/36475065/8733066)

import ipaddress
base_address = "192.168.0.1"
subnet_bits = 8
addr_int = int(ipaddress.IPv4Address(base_address))
max_addr = addr_int + 2**subnet_bits
to_ping = [str(ipaddress.IPv4Address(i_addr)) for i_addr in range(addr_int, max_addr)]
def is_up(ip_address):
    return os.system("ping -c 1 -w 2 "+ip_address)==0
online_hosts = list(filter(is_up, to_ping))
# do whatever you want with online_hosts
bb1950328
  • 1,403
  • 11
  • 18
  • 1
    On most linux systems the `ping` program that comes preinstalled will keep running until it is killed with a CTRL+C. In those cases your program will never terminate. It's best to use the `-c` & `-w` option to make sure that the `ping` command exits. – rdas Oct 14 '19 at 14:33