Networking

Unix and Linux network configuration. Multiple network interfaces. Bridged NICs. High-availability network configurations.

Applications

Reviews of latest Unix and Linux software. Helpful tips for application support admins. Automating application support.

Data

Disk partitioning, filesystems, directories, and files. Volume management, logical volumes, HA filesystems. Backups and disaster recovery.

Monitoring

Distributed server monitoring. Server performance and capacity planning. Monitoring applications, network status and user activity.

Commands & Shells

Cool Unix shell commands and options. Command-line tools and application. Things every Unix sysadmin needs to know.

Home » Networking

Find Available IP Addresses

Submitted by on June 18, 2015 – 10:58 am

After a while, available IP addresses on the subnet may become hard to come by. Whatever spreadsheet you used to track IP allocations is likely out of date. Here’s a simple script that will scan the specified subnet and give you a list of IPs that don’t ping and have no associated DNS records. This should help you narrow down your choices.


#!/bin/bash
# .. .. .. .. .. .. .. .. .. ...  .. .. .. .. .. .. .. .. .. .. .. .. ...
# :             (              :  : Identify available IP addresses on  :
# :            ( ,)            :  : the subnet                          :
# :           ). ( )           :  :                                     :
# :          (, )' (.          :  :                                     :
# :         \WWWWWWWW/         :  :                                     :
# :          '--..--'          :  :                                     :
# :             }{             :  :                                     :
# :             {}             :  :                                     :
# : |/ _ _ _  \    / _  _|  _  :  :                                     :
# : |\| (_|/_\/\/\/ (_)| |<_\  :  :                                     :
# :          /                 :  :                                     :
# :     www.krazyworks.com     :  :                                     :
# ... .. .. .. .. .. .. .. .. ..  ... .. .. .. .. .. .. .. .. .. .. .. ..
# .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. ..
# Copyright 2015, KrazyWorks LLC                                        :
#                                                                       :
# This program is free software: you can redistribute it and/or modify  :
# it under the terms of the GNU General Public License as published by  :
# the Free Software Foundation, either version 3 of the License, or     :
# (at your option) any later version.                                   :
#                                                                       :
# This program is distributed in the hope that it will be useful,       :
# but WITHOUT ANY WARRANTY; without even the implied warranty of        :
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         :
# GNU General Public License for more details.                          :
#                                                                       :
# You should have received a copy of the GNU General Public License     :
# along with this program.  If not, see <http://www.gnu.org/licenses/>. :
# .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. ..

if [ ! "" ]
then
	echo "Specify subnet like so: 192.168.1"
	exit 1
fi

subnet=""

configure() {
	tmpfile="/tmp/free_ip_scan.tmp"
}

cleanup() {
	if [ -f "${tmpfile}" ]
	then
		/bin/rm -f "${tmpfile}"
	fi
}

is_alive_ping()
{
	ping -c 4 $1 >/dev/null 2>&1
	[ $? -ne 0 ] && echo $1
}

pingdo() {
	for i in ${subnet}.{10..250}
	do
		is_alive_ping $i & disown
	done | sort -t. -k4 -n > "${tmpfile}"
}

digdo() {
	if [ -r "${tmpfile}" ]
	then
		echo "The following IPs are not pingable and are not in DNS, so they *may* be available:"
		for ip in `cat "${tmpfile}"`
		do
			if [ `dig +short -x "${ip}" 2>/dev/null | wc -l` -eq 0 ]
			then
				echo "${ip} seems available"
			fi
		done | column -t
	else
		echo "Scraped the bottom of the pickle barrel but came up dry"
	fi
}

# RUNTIME

configure
cleanup
pingdo
digdo
cleanup

 

Print Friendly, PDF & Email

Leave a Reply