bashshellsolaris-10

Shell script to check whether a server is reachable?


I have 5 Solaris servers present across different locations. Sometimes some of these servers are not reachable from my location due to various reasons (either because of network problems or the server itself goes down suddenly).

So I would like to write a Bash shell script to check wether they are reachable. What I have tried is:

ssh ipaddress "uname -a"

Password less authentication is set. If I don't get any any output I will generate a mail.

  1. Are there any otherways to check server reachability?
  2. Which one is the best way?
  3. Is what I have tried correct?

Solution

  • You can use ping -c4 $ip_address where $ip_address is the ip of your remote server and parse the output to capture the successful packets and/or failed packets and use mail -s to send the log via email.

    Here is something to get you started and you can build on it.

    ping -c4 www.google.com | awk '/---/,0'
    

    This will give an output like this -

    [jaypal:~/Temp] ping -c4 www.google.com | awk '/---/,0'
    --- www.l.google.com ping statistics ---
    4 packets transmitted, 4 packets received, 0.0% packet loss
    round-trip min/avg/max/stddev = 36.638/36.870/37.159/0.196 ms
    

    I checked the Solaris man-page for ping. Output from ping on Solaris box is different. Also, on Linux you limit the packets by stating -c and number of packets. On Solaris you would have to do -

    ping -s www.google.com 2 4
    
    /usr/sbin/ping -s [-l | -U] [-adlLnrRv] [-A addr_family]
    [-c traffic_class] [-g gateway [ -g gateway...]] [-
    F flow_label] [-I interval] [-i interface] [-P tos] [-
    p port] [-t ttl] host [data_size] [npackets]
                               ^           ^
                               |           |
    ---------------------------------------  
    

    Unfortunately I don't have a solaris box handy to help you out with.