Ping certain range of ip address

hey guys,
In my program i am giving an initial ip & end ip.
i just want to check every ip in that range is pingable or not

echo "Enter the initial ip:"
read inip
echo "Enter the end ip:"
read endip
for (( i=0; i<=$endip; i++ ))
do
if ping -c 4 $inip
then
echo "pingable"
else
echo "not pingable"
fi
done
echo "Done"

my initial ip is 172.30.36.1
my end ip is 172.30.36.10
How do i increment the last digits in a loop

Try

BIP1=$((0x$(printf "%02X" ${inip//./ })));
BIP2=$((0x$(printf "%02X" ${endip//./ })));
for ((I=BIP1; I<=BIP2; I++))
  do ping $(printf "%d.%d.%d.%d\n" $((I>>24)) $((I>>16&255)) $((I>>8&255))  $((I&255)) )
  done

This is how i tried

echo "Enter the initial ip:"
read inip
echo "Enter the end ip:"
read endip
a= echo $inip | awk -F. '{print $4}'
b= echo $endip | awk -F. '{print $4}'
c= echo $inip | head -c10

pingfunc ()
{
for (( i=$a; i<=$b; i++ ))
do 
if ping -c 4 $c$i
then
echo "pingable"
else
echo "not pingable"
fi
done
}
pingfunc
echo "Done"

but when i try to run it shows me an error
./pingtest.sh: line 11: ((: i=: syntax error: operand expected (error token is "=")
Help me out from this

Your variable assignment doesn't work that way:

  • no space after the = sign
  • use "command substitution" $(...) for the pipes

You could use shell's "parameter expansion" (instead of above pipes) to extract patterns/substrings from the variables for the assignments.

1 Like

Hey Rudic,
Thanks for the information
As u said there should be no space after the = sign
i just changed the code like this,its working as expected

echo "Enter the initial ip:"
read inip
echo "Enter the end ip:"
read endip
a=$(echo $inip | awk -F. '{print $4}')
b=$(echo $endip | awk -F. '{print $4}')
c=$(echo $inip | head -c10)

pingfunc ()
{
for (( i=$a; i<=$b; i++ ))
do 
if ping -c 4 $c$i
then
echo "pingable"
else
echo "not pingable"
fi
done
}
pingfunc
echo "Done"



Try also

IP1=172.30.36.1
IP2=172.30.36.10
for ((i=${IP1##*.}; i<=${IP2##*.}; i++)); do echo ping -c4 ${IP1%.*}.$i; done
1 Like