简体   繁体   English

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

[英]Bash Script to loop through list to check server status

I'm trying to create a script that will ping amongst other things, remote servers held in a list.我正在尝试创建一个脚本,该脚本将在列表中的远程服务器中执行 ping 操作。

This is what i have so have, but keep getting this error:这就是我所拥有的,但不断收到此错误:

./monitor_sats.sh: cannot make pipe for command substitution: Too many open files. ./monitor_sats.sh:无法为命令替换制作管道:打开的文件太多。

This is my code, thanks for helping.这是我的代码,感谢您的帮助。

 #!/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) "

change function name as below;更改函数名称如下;

#!/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) "

you can also use this;你也可以使用这个;

#!/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

This script looks syntactically correct but there is something wired: why ping the same list of servers for different cities ?这个脚本在语法上看起来是正确的,但有一些问题:为什么要为不同的城市 ping 相同的服务器列表?

This make me feel you don't give the whole context...这让我觉得你没有给出整个背景......

Also, can you give the output of ulimit -a ?另外,你能给出ulimit -a的输出吗? and tell the size of the list ?并告诉列表的大小?

for i in $(cat list_server) ; 
do ping -c 1 -w 1 $i > /dev/null && echo "$i - OK" || echo "$i - NOK" ; 
done

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

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