gpt4 book ai didi

linux - Bash 脚本循环遍历列表以检查服务器状态

转载 作者:太空宇宙 更新时间:2023-11-04 10:30:22 28 4
gpt4 key购买 nike

我正在尝试创建一个脚本,除其他外,该脚本将 ping 列表中的远程服务器。

这就是我所拥有的,但不断出现此错误:

./monitor_sats.sh:无法为命令替换创建管道:打开的文件太多。

这是我的代码,感谢您的帮助。

 #!/bin/bash


function ping {

for i in `cat server_list`
do
ping -c1 ${i} > /dev/null
if [ ${?} -eq 0 ]; then
echo "$(tput setaf 2)ON$(tput sgr0)"
else
echo "$(tput setaf 1)OFF$(tput sgr0)"
fi
done
}



echo "AMSTERDAM - Server $(ping) "
echo "HONG KONG - Server $(ping) "
echo "LONDON - Server $(ping) "
echo "SINGAPORE - Server $(ping) "

最佳答案

如下更改函数名;

#!/bin/bash
function pingToServer {
for i in `cat server_list`
do
ping -c1 ${i} > /dev/null
if [ ${?} -eq 0 ]; then
echo "$(tput setaf 2)ON$(tput sgr0)"
else
echo "$(tput setaf 1)OFF$(tput sgr0)"
fi
done
}

echo "AMSTERDAM - Server $(pingToServer) "
echo "HONG KONG - Server $(pingToServer) "
echo "LONDON - Server $(pingToServer) "
echo "SINGAPORE - Server $(pingToServer) "

你也可以用这个;

#!/bin/bash
Countries=("AMSTERDAM" "HONG KONG" "LONDON" "SINGAPORE")
counter=0
cat server_list | while read server;
do
ping -c1 ${server} > /dev/null
if [ ${?} -eq 0 ]; then
echo "${Countries[$counter]} - SERVER- $(tput setaf 2)ON$(tput sgr0)"
else
echo "${Countries[$counter]} - SERVER-$(tput setaf 1)OFF$(tput sgr0)"
fi
counter=$(($counter+1))
done

关于linux - Bash 脚本循环遍历列表以检查服务器状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40258362/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com