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.
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"
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
./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 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: