通过ping探测主机在线状态
#!/bin/env sh
source /etc/rc.d/init.d/functions
declare -i p=1
declare -i online=0
declare -i offline=0
net='192.168.8'
while [[ $p -le 10 ]];do
ping -c 2 -W 1 $net.$p >& /dev/null
if [ $? -eq 0 ];then
action "$net.$p online" true
let online++
else
action "$net.$p offline" false
let offline++
fi
let p++
done
echo -e "\033[31;44;5m online hosts:$online\033[0m"
echo -e "\033[32;46;4m offline hosts:$offline\033[0m"

在此想深究ping的-w和-W选项,极易混淆
-w deadline
Specify a timeout, in seconds, before ping exits regardless of how many packets have been sent or received. In this case ping does not stop
after count packet are sent, it waits either for deadline expire or until count probes are answered or for some error notification from
network.
-W timeout
Time to wait for a response, in seconds. The option affects only timeout in absence of any responses, otherwise ping waits for two RTTs.
-w指的是总的超时时间,而-W指的是每次ping包收不到相应的超时时间,默认两个RTT,区别还是很大,此外在脚本中使用ping必须-c或-w指定ping终止条件,否则脚本卡在ping命令上

浙公网安备 33010602011771号