#!/bin/bash # lan_ip_tester.sh # Meryll Larkin September 13, 2014 # corrected December 21, 2018 # Put your local network designation in the LAN variable in this format: # LAN='192.168.1.0/24-LAN1 10.0.0.0/28-LAN2' # Each LAN written in slash subnet notation, followed by a dash and a label # A space separated list. # The entire list enclosed by single quotes. LAN='192.168.1.0/24-LAN1 10.0.0.0/28-LAN2' ostype=`uname -o` # Cygwin # GNU/Linux (ubuntu) now=`date +%Y%m%d%H%M | tr -d '\r|\n'` end='.txt' ofile="ping_output_$now" outfile="$ofile$end" if [[ $ostype = Cygwin* ]]; then myping="ping -n 1" else myping="ping -B -W 3 -t 1 -c 1" fi function error() { echo $1 echo "Exiting." exit 1 } function get_avail_ips() { case $subnet in 8) num_ips="16777216" ;; 9) num_ips="8388608" ;; 10) num_ips="4194304" ;; 11) num_ips="2097152" ;; 12) num_ips="1048576" ;; 13) num_ips="524288" ;; 14) num_ips="262144" ;; 15) num_ips="131072" ;; 16) num_ips="65536" ;; 17) num_ips="32768" ;; 18) num_ips="16384" ;; 19) num_ips="8192" ;; 20) num_ips="4096" ;; 21) num_ips="2048" ;; 22) num_ips="1024" ;; 23) num_ips="512" ;; 24) num_ips="256" ;; 25) num_ips="128" ;; 26) num_ips="64" ;; 27) num_ips="32" ;; 28) num_ips="16" ;; 29) num_ips="8" ;; 30) num_ips="4" ;; 31) num_ips="2" ;; 32) num_ips="1" ;; esac } for lan in $LAN ; do subnote=`echo $lan | cut -d'.' -f4` # echo "LAN with subnotation = $lan" # echo "last octet and subnotation = $subnote" lastoct=`echo $subnote | cut -d'/' -f1` # test that starting ip address is a valid one if [ $lastoct -gt 256 ] ; then error "Invalid ip address $LAN $lastoct" fi firstoct=`echo $lan | cut -d'.' -f1` if [ $firstoct -gt 256 ] ; then error "Invalid ip address $LAN $firstoct" fi secondoct=`echo $lan | cut -d'.' -f2` if [ $secondoct -gt 256 ] ; then error "Invalid ip address $LAN $secondoct" fi thirdoct=`echo $lan | cut -d'.' -f3` if [ $thirdoct -gt 256 ] ; then error "Invalid ip address $LAN $thirdoct" fi subnetv=`echo $subnote | cut -d'/' -f2` # subnetv and subnet will be different if multiple LANs are supplied subnet=`echo $subnetv | cut -d'-' -f1` vnet=`echo $subnetv | cut -d'-' -f2` # echo "lastoct = $lastoct" # echo -n "subnet = $subnet " # echo -n "vnet = $vnet " # call subroutine to get the number of available IP addresses in each LAN get_avail_ips netoct1=`echo $lan | cut -d'.' -f4 --complement` echo "netoct1 = $netoct1" netoct=`echo "$netoct1" | sed 's/$/./'` # echo "netoct = $netoct" # echo "num_ips = $num_ips" # echo "netoct lastoct = $netoct$lastoct" for i in $(eval echo "{0..$num_ips}"); do # echo "num_ips = $num_ips i = $i" echo "pinging $netoct$lastoct" echo -n "$vnet $netoct$lastoct ">>$outfile # $myping $netoct$lastoct > /dev/null && echo "UP" >>$outfile || : & echo "DOWN" >>$outfile $myping $netoct$lastoct > /dev/null if [ $? -eq 0 ] ; then echo "UP" >>$outfile else echo "DOWN" >>$outfile fi ((lastoct = lastoct + 1)) if [ $lastoct -eq 256 ] ; then ((lastoct = 0)) ((thirdoct = thirdoct +1)) fi if [ $thirdoct -eq 256 ] ; then ((thirdoct = 0)) ((secondoct = secondoct +1)) fi if [ $secondoct -eq 256 ] ; then ((secondoct = 0)) ((firstoct = firstoct +1)) fi if [ $firstoct -eq 256 ] ; then error "$netoct$lastoct plus one is not a valid ip address" fi netoct=${firstoct}.${secondoct}.${thirdoct}. done done