Christian Watts Christian Watts - 1 month ago 13
Linux Question

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

Answer

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