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