linux - Ping 监控 Bash 中的逗号分隔文件?

标签 linux 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 地址 仅一次

ret=$?

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

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

测试 ping 是否成功

echo "$client: online"

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

done < clients

逐行读取文件的惯用结构

关于linux - Ping 监控 Bash 中的逗号分隔文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56954096/

相关文章:

linux - 如何在bash中向后台发送ssh命令

bash - 如果 ( ) </开发/空

bash - 'G' 与 sed 命令有什么关系?

python - 在 bash 管道中使用时,保留 Jenkins 中 python 脚本的返回代码

bash - 错误 : EACCES, 权限被拒绝 Ionic Cordova IOS

linux - 从 ttyUSB0 读取输出

c# - memcached - 与 C# asp.net 应用程序一起使用

linux - Mspdebug 收不到数据

javascript - 使用 node.js 连接到已经建立的 UNIX 套接字?

c - 父子进程之间的信号量