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.
 
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
hostlist.txt · Last modified: by steve