Christian Watts
Christian Watts

Reputation: 75

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.

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.

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) "

Upvotes: 0

Views: 3057

Answers (3)

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

Upvotes: 0

Mustafa DOGRU
Mustafa DOGRU

Reputation: 4112

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

Upvotes: 1

Setop
Setop

Reputation: 2490

This script looks syntactically correct but there is something wired: why ping the same list of servers for different cities ?

This make me feel you don't give the whole context...

Also, can you give the output of ulimit -a ? and tell the size of the list ?

Upvotes: 0

Related Questions