Shell script to ping multiple servers

Hi

I did the following script to ping multiple servers, but I keep on receiveing duplicate emails for one server that is down:

#!/bin/bash

date
cat /var/tmp/servers.list |  while read output
do
    ping -c 1 "$output" > /dev/null
    if [ $? -eq 0 ]; then
    echo "node $output is up"
    else
    echo "node $output is down" | mailx -s "$output not pinging" xyz@klm.com
    fi
done

So the first email I received had body as "node is down" and subject "not pinging"
and second email I received had body as "node 192.168.5.50 is down" and subject "192.168.5.50 not pinging".
So I have 13 ips on the servers.list , so the only server down is 192.168.6.50

I guess you have an empty line in your list.
The following discards empty lines and #comments and trailing space characters:

#!/bin/bash

date
while read output junk <&3
do
  case $output in
  "#"*|"")
  ;;
  *)
    ping -c 1 "$output" > /dev/null
    if [ $? -eq 0 ]; then
      echo "node $output is up"
    else
      echo "node $output is down" | mailx -s "$output not pinging" xyz@klm.com
    fi
  ;;
  esac
done 3< /var/tmp/servers.list

Sounds like you have an empty first line in servers.list (which you unfortunately didn't post). As usual, an incomplete specification yields suboptimal proposals/solutions.

1 Like

Are these servers in a reasonable subnet? e.g. 10.1.1.x

If they are, then you might consider nmap which can ping a range of servers. Beware that by default it will probe all sorts of ports, which might not be what you want and can cause anti-intrusion alerts to be triggered.

Do not do this to public internet servers else your ISP or others may cut you off.

Something like this might do the trick:

nmap -n -v -oG - -sn ip-range

You will need to confirm the correct flags for your implementation. The IP range can be a string such as (in the example above) 10.1.1.0/24 so it will scan the whole class C subnet.

Going with a smaller netmask (i.e. the /24 reducing to /23 or less) will exponentially increase the number of ip addresses tried and exponentially increase the execution time.

It will also increase the likelihood of triggering anti-intrusion alerts, which may be "detrimental to your career prospects"

Robin

After correcting the mistake of an empty line I am having this error:
Invalid operation mode f

 ./netcheck.sh
Friday, March 17, 2017 02:24:56 PM CAT
node 192.168.6.4 is up
node 192.168.6.41 is up
node 192.168.6.24 is up
node 192.168.6.11 is up
node 192.168.6.108 is up
node 192.168.6.26 is up
node 192.168.6.51 is up
node 192.168.6.8 is up
node 192.168.6.9 is up
Invalid operation mode f
node 192.168.6.25 is up
node 192.168.6.26 is up
node 192.168.6.71 is up
node 192.168.6.107 is up

I shoud receive an email because on file servers.list I deliberatly inserted a wrong IP address:

 more servers.list
192.168.6.4
192.168.6.41
192.168.6.24
192.168.6.11
192.168.6.108
192.168.6.26
192.168.6.51
192.168.5.50
192.168.6.8
192.168.6.9
192.168.6.25
192.168.6.26
192.168.6.71
192.168.6.107
You have new mail in /var/mail//root

Can you:-

  • Post what your code looks like now
  • Track down which command is being called in which way that generates this error
    Using set -x at the top of your script will help with this

Otherwise is is crystal ball time, and mine is a bit out of focus.

Robin

this is the output of the script with set -x

 ./netcheck.sh
+ date
Friday, March 17, 2017 09:10:54 PM CAT
+ cat /var/tmp/servers.list
+ read output
+ ping -c 1 192.168.6.4
+ '[' 0 -eq 0 ']'
+ echo 'node 192.168.6.4 is up'
node 192.168.6.4 is up
+ read output
+ ping -c 1 192.168.6.41
+ '[' 0 -eq 0 ']'
+ echo 'node 192.168.6.41 is up'
node 192.168.6.41 is up
+ read output
+ ping -c 1 192.168.6.24
+ '[' 0 -eq 0 ']'
+ echo 'node 192.168.6.24 is up'
node 192.168.6.24 is up
+ read output
+ ping -c 1 192.168.6.11
+ '[' 0 -eq 0 ']'
+ echo 'node 192.168.6.11 is up'
node 192.168.6.11 is up
+ read output
+ ping -c 1 192.168.6.108
+ '[' 0 -eq 0 ']'
+ echo 'node 192.168.6.108 is up'
node 192.168.6.108 is up
+ read output
+ ping -c 1 192.168.6.26
+ '[' 0 -eq 0 ']'
+ echo 'node 192.168.6.26 is up'
node 192.168.6.26 is up
+ read output
+ ping -c 1 192.168.6.51
+ '[' 0 -eq 0 ']'
+ echo 'node 192.168.6.51 is up'
node 192.168.6.51 is up
+ read output
+ ping -c 1 192.168.5.50
+ '[' 1 -eq 0 ']'
+ echo 'node 192.168.5.50 is down'
+ mailx -s '192.168.5.50 not pinging' xyz@asdf.com -b xyz@gmail.com
+ read output
+ ping -c 1 192.168.6.8
+ '[' 0 -eq 0 ']'
+ echo 'node 192.168.6.8 is up'
node 192.168.6.8 is up
+ read output
+ ping -c 1 192.168.6.9
+ '[' 0 -eq 0 ']'
+ echo 'node 192.168.6.9 is up'
node 192.168.6.9 is up
+ read output
+ ping -c 1 192.168.6.25
Invalid operation mode f
+ '[' 0 -eq 0 ']'
+ echo 'node 192.168.6.25 is up'
node 192.168.6.25 is up
+ read output
+ ping -c 1 192.168.6.26
+ '[' 0 -eq 0 ']'
+ echo 'node 192.168.6.26 is up'
node 192.168.6.26 is up
+ read output
+ ping -c 1 192.168.6.71
+ '[' 0 -eq 0 ']'
+ echo 'node 192.168.6.71 is up'
node 192.168.6.71 is up
+ read output
+ ping -c 1 192.168.6.107
+ '[' 0 -eq 0 ']'
+ echo 'node 192.168.6.107 is up'
node 192.168.6.107 is up
+ read output

I don't see any Invalid operation mode f in the output above...[/FONT][/COLOR]

If I remove set -x I got this:

 ./netcheck.sh
Saturday, March 18, 2017 10:05:32 PM CAT
node 192.168.6.4 is up
node 192.168.6.41 is up
node 192.168.6.24 is up
node 192.168.6.11 is up
node 192.168.6.108 is up
node 192.168.6.26 is up
node 192.168.6.51 is up
node 192.168.6.8 is up
node 192.168.6.9 is up
Invalid operation mode f
node 192.168.6.25 is up
node 192.168.6.26 is up
node 192.168.6.71 is up
node 192.168.6.107 is up

I think the error message is from your sendmail.

I would suggest the use of fping with the mask option, since you are not restricting yourself in ping.

This will ping all the ip in given network

fping -g 192.168.1.0/24

The response will be easy to parse in a script:

192.168.1.1 is alive
192.168.1.2 is alive
192.168.1.3 is alive
192.168.1.5 is alive
...
192.168.1.4 is unreachable
192.168.1.6 is unreachable
192.168.1.7 is unreachable
...

Note: Using the argument -a will restrict the output to reachable ip addresses, you may want to use it otherwise fping will also print unreachable addresses:

fping -a -g 192.168.1.0/24

Regards
Christian
System Admin
Cloud Hosted Virtual Desktop