Free IP address script

We frequently have to assign static IP addresses to servers and certain workstations. Although the following script does not guarantee that the IPs returned are not currently in use or are reserved for future use, it performs the following checks:

* DNS PTR (reverse) lookup
* TCP Connect scan using nmap for common services

If an IP address does not respond to a ping, does not have a DNS PTR record, and is not listening for common services, it is considered available for assignment.


# For each of the IPs specified, try an ICMP ECHO REQUEST (ping),
# reverse DNS (PTR) lookup, and port scan for open $SERVICES.
# If the host does not respond to a ping, has no DNS PTR entry,
# and does not have open ports listed in the $SERVICES variable,
# it is considered available for assignment.

if [ -z $@ ] ; then
   echo "  Usage: ./ {IP_range}"
   echo "Example: ./"
   exit 1

# SERVICES is the list of ports to scan on the host

for i in `nmap -sP -v $1 | grep down | awk '{print $2}' | tr -d '()'`
   # Does a DNS PTR record exist?
   dig -x $i | grep NXDOMAIN > /dev/null 2>&1
   if [ $? -eq 0 ] ; then
      # Are any of the ports listed in $SERVICES open?
      nmap -P0 -p $SERVICES -T Aggressive $i | grep open > /dev/null 2>&1
      if [ $? -eq 1 ] ; then
         echo $i

Back to

Last modified: 05/08/2003