Visit The New, Modern Unix Linux Community


Shell script to ping multiple servers


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell script to ping multiple servers
# 1  
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:
Code:
#!/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
# 2  
I guess you have an empty line in your list.
The following discards empty lines and #comments and trailing space characters:
Code:
#!/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

# 3  
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.
This User Gave Thanks to RudiC For This Post:
# 4  
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:
Code:
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
# 5  
After correcting the mistake of an empty line I am having this error:
Invalid operation mode f

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


Last edited by fretagi; 03-17-2017 at 09:30 AM.. Reason: insert a line
# 6  
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
# 7  
this is the output of the script with set -x
Code:
 ./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


Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #996
Difficulty: Medium
Major Unix vendors, including Sun Microsystems, HP, IBM, and SGI, have been selling virtualized hardware since before 1990.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script for connecting multiple servers and then copying 30 days old files

Shell script for connecting multiple servers and then copying 30 days old files from those server . HI , I have 6 multiple servers pla1,pla2,pla3,pla4,pla5,pla6 1. These six servers have common shared mount point /var/share 2. Running script from /var/share to connect these servers.I... (1 Reply)
Discussion started by: rcroyal88
1 Replies

2. Shell Programming and Scripting

Need shell script to Telnet multiple node , Ping some IP and print output

Hi Team, Need shell script to Telnet multiple node , Ping some IP and print output like pass or fail. Need this script to check reachability of multiple nodes at same time. Help me. I use this but not working... Eg. in this script i need to telnet... (4 Replies)
Discussion started by: Ganesh Mankar
4 Replies

3. Shell Programming and Scripting

Ping script to list of servers

Hi Friends, I have experience in redhat/ Ubuntu OS, but I am very new to solaries os. my servers OS is Oracle Solaris 10 8/11 s10x_u10wos_17b X86. I have a file contains 200 servers IPs one by one. now I want a script to chaeck which IPs are pinging, not pingning. I... (8 Replies)
Discussion started by: kumar85shiv
8 Replies

4. Shell Programming and Scripting

Script to Ping Servers

Hey, It's me again! Still trying to learn to become a better scripter on the job :) New challenge for assistance, if anyone cares to help, and its two parted! First part, I wanted to create a script at work that would ping a server that was supplied in an argument, then a count (amount of times)... (5 Replies)
Discussion started by: gkelly1117
5 Replies

5. Shell Programming and Scripting

Shell script to connect to multiple ssh servers

Hello, I have access to several linux servers (mostly centos based) located in a DC in another country. from day to day I need to login to each of them to do some work (they dont have gui/window manager installed, I work only from console), or even to just do a check like df -h for disc usage.... (3 Replies)
Discussion started by: MaRiOsGR
3 Replies

6. Shell Programming and Scripting

run vi/vim encrypted shell script without decryption on multiple servers

Hello Everyone, How do we run vi/vim encrypted shell script without decryption on multiple servers. It is a simple bash script and vim -nx <filename> has been used to encrypt with desired password. Now I have few errors, the syntax is absolutely fine as I have run that script multiple times on... (0 Replies)
Discussion started by: lovesaikrishna
0 Replies

7. Shell Programming and Scripting

Ping the hostname of many servers using a script

Hi We have some 300 servers in the Data center and some of them are running with AIX and some of them are running with Solaris. I need a script which can be run in one of the server and that script should ping the hostname of all the 300 servers. Also the script should notify if any server is... (9 Replies)
Discussion started by: newtoaixos
9 Replies

8. Shell Programming and Scripting

Shell script to change the password on multiple servers in linux

Can any one please let me know the shell script to change the password for a particular user on multiple linux servers. (2 Replies)
Discussion started by: s_madras
2 Replies

9. Shell Programming and Scripting

Prevent wrong user from using shell script for multiple remote servers

Hi, I am running a shell script from a central server to multiple remote servers using the following code: application_check() { # Linux/UNIX box with ssh key based login SERVERS=`cat /tmp/server-details` # SSH User name USR="user" # create new file > /tmp/abc.log # connect... (2 Replies)
Discussion started by: mystition
2 Replies

10. UNIX for Advanced & Expert Users

script to ping servers

Hi , I would like to automate a script to ping all the unix servers perodically thru cronjob. Is there any script out there? If so Please give me. Thanks in advance. (2 Replies)
Discussion started by: sriny
2 Replies

Featured Tech Videos