有時候想看看網段中有哪些IP被用了,能夠用這個快速ping作一個初步的判斷。能夠本身指定測試IP網段,並將測試結果整理排序到文件。shell
#!/bin/bashbash
#V1.0 2019-09-10ide
#Ping test shell script by tutor測試
clear排序
>ip-up.txtip
>ip-down.txtinput
while true;it
doio
read -p "Please input the ip segment for ping test(like 192.168.100) : " segmentclass
if [ -z $segment ] ; then
segment="192.168.100"
fi
echo -n "the ip segment is ${segment} , are you sure to continue [y/n] ? "
read action
if [ -z $action ] ; then
break
fi
if [ "$action" = "y" ]; then
break
fi
done
echo "ping test is ready to run! "
for i in {1..254}
do
{
ping -c2 -W1 ${segment}.${i} &>/dev/null
if [ $? -eq 0 ]; then
echo "${segment}.$i is up" &>/dev/null >> ip-up.txt
else
echo "${segment}.$i is down" &>/dev/null >> ip-down.txt
fi
}&
done
wait
sort -n -k 4 -t . ip-up.txt -o ip-up.txt
cat ip-up.txt
sort -n -k 4 -t . ip-down.txt -o ip-down.txt
cat ip-down.txt
echo "ping test are finished!"