User Tools

Site Tools


hostlist

HostList

HostList
#!/usr/bin/bash
#  Get a list of hosts from the DHCP server on the router
#  SRJ 2026-08-13
#
 
Router="192.168.1.1"
#  Set the address of the router/dhcp server above.
#  For almost everyone, this should be the only change 
#  needed for this script.
## This could be automated with;
## Router="$(ip route show default | grep -oE "\b([0-9]{1,3}\.){3}[0-9]{1,3}\b" | head -1)"
 
Sub="$(echo "${Router}" | cut -d "." -f 1-3)"
#  This gets the base range to scan
 
#  If you need to change the range of addresses, see the table 
#  at the bottom of this script.
for Ip in $(seq 1 254)
	do
	Host="$(host ${Sub}.${Ip} ${Router} | grep pointer)"
	if [[ ${Host} ]] then
		Name="$(echo ${Host} | cut -d " " -f 5 | cut -d "." -f1)"
		echo -e "${Sub}.${Ip}\t${Name}"
	fi
done
#  This loop runs the host command asking if the dhcp server has an 
#  active lease for each addess in turn. It then emits the IP address
#  followed by a tab and the name of the host
 
#  For the differernt subnet sizes, the number of addresses to scan
#  changes, if the subnet is 255.255.255.0 or /24 there should be
#  a total of 255 addresses, one of which is the router, and one
#  being the broadcast address.
#  
#  Adjust the low and high in the $(seq # ###) bit above to deal with
#  that variation.
#  
#  CIDR	Addr.	Hosts	Netmask		
#  /30	4	2	255.255.255.252
#  /29	8	6	255.255.255.248
#  /28	16	14	255.255.255.240
#  /27	32	30	255.255.255.224
#  /26	64	62	255.255.255.192
#  /25	128	126	255.255.255.128
#  /24	256	254	255.255.255.0
#  /23	512	510	255.255.254.0
#  /22	1024	1022	255.255.252.0
#  /21	2048	2046	255.255.248.0
#  /20	4096	4094	255.255.240.0
#  /19	8192	8190	255.255.224.0
#  /18	16384	16382	255.255.192.0
#  /17	32768	32766	255.255.128.0
#  /16	65536	65534	255.255.0.0

The output this generates will be similar to;

192.168.1.1	router
192.168.1.12	HDHR-10605406
192.168.1.13	nuc
192.168.1.14	srj-dock
192.168.1.15	mana-pi
192.168.1.18	Tiny
192.168.1.22	Hue
192.168.1.55	fred
192.168.1.68	C201
192.168.1.89	homeassistant
192.168.1.101	Navimow_i
192.168.1.108	iRobot-C9154212A6BC437F12CE21F4451A66
192.168.1.172	iPhone
192.168.1.174	octopi
192.168.1.179	SRJ-Phone
192.168.1.194	Sensi-1F38E9
192.168.1.200	Living-Room-TV
192.168.1.251	AP-1
192.168.1.252	AP-2
hostlist.txt · Last modified: by Steve Jones