ping某网段是否连通脚本(案例)
#!/bin/bash
for i in {1..254}
do
ping -c2 -i0.3 -W1 192.168.4.$i &>/dev/null
if [ $? -eq 0 ];then
echo "192.168.4.$i is up"
else
echo "192.168.4.$i is down"
fi
done
或者利用数组的形式(只是记住用法)
#!/bin/bash
IP=(192.168.20.11 192.168.20.12 192.168.20.13)
for IP in ${IP[*]}; do
ping -c1 -i0.2 $IP > /dev/null
if [ $? -eq 0 ]; then
echo "$IP UP"
else
echo "$IP DOWN"
fi
done
又或者
#!/bin/bash
IP=(192.168.20.11 192.168.20.12 192.168.20.13)
for ((i=0;i<${#IP[*]};i++)); do
ping -c1 -i0.2 ${IP[$i]} > /dev/null
if [ $? -eq 0 ]; then
echo "${IP[$i]} UP"
else
echo "${IP[$i]} DOWN"
fi
done