通过ping探测主机在线状态

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#!/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命令上

posted @   ascertain  阅读(431)  评论(0编辑  收藏  举报
编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
点击右上角即可分享
微信分享提示