繁体   English   中英

在 Bash 中 Ping 监视逗号分隔的文件?

[英]Ping monitoring for a comma separated file in Bash?

我正在尝试为 OpenVPN 客户端设备创建 ping 监控。

我想从 IP 列表 ping 所有设备。

.txt文件如下所示:

client1,10.8.0.2
client2,10.8.0.3
client3,10.8.0.3
.. and so on 

我希望 bash 脚本创建一个 .txt 文件,其中包含以下内容:

client1: online
client2: offline
client3: online
and so on.

这怎么可能?

提前致谢!

为了我的需要,我使用这个脚本:

#! /bin/bash
while read -r client address
do
    ping -c 1 $address
    ret=$?
    if [[ 0 -eq $ret ]] 
    then
        echo "$client: online"
    else
        echo "$client: off line"
    fi
done < clients

和:

IFS=,

将逗号设置为字段分隔符

while read -r foo bar

读取文件并将字段存储在clientadrress变量中

pinc -c 1 address

ping address一次

ret=$?

获取ping命令返回值(一切正常时为 0)

如果 [[ 0 -eq $ret ]] ....

测试ping是否成功

echo "$client: online"

给出ping结果,在线或离线打印

done < clients

逐行读取文件的惯用结构

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM