LINUX Ping host, Display error on failure

6,316

Solution 1

First, you should add 2>/dev/null to the ping invocation, so that error messages from ping would not be printed to the standard error.

Second, $? in your code will not contain the result you expect, because the return status of a pipeline is the exit status of the last command, which is gawk in your case, and the exit status of ping is just ignored. You could rewrite the code, e.g., like this:

output=$(ping -c 1 "$input" 2>/dev/null)
if [ $? -eq 0 ]; then
    ip=$(printf '%s' "$output" | gawk -F'[()]' '/PING/{print $2}')  
    echo "$ip is up";
else
    echo "host is down";
fi

Solution 2

I think awk is not necessary. Unless I'm missing something that code should do the trick:

#!/bin/bash

host=$1
ping -c1 $host > /dev/null 2> /dev/null
[[ $? == 0 ]] && echo "$host is up" || echo "$host is down/not reachable"

Here an example:

$ ./checkping www.google.com
www.google.com is up
$ ./checkping www.google.utld
www.google.utld is down/not reachable
Share:
6,316

Related videos on Youtube

ICE
Author by

ICE

Hi, I like problem solving. Also, beer. I like beer.

Updated on September 18, 2022

Comments

  • ICE
    ICE almost 2 years

    I'm writing a bash script to ping a given hostname, and display whether or not the host is active (display a simple message.) Should be easy, but headache instead. Here is what I have so far:

        echo & echo "DOI (Domain):" &&read input 
        ip=$(ping -c 1 $input | gawk -F'[()]' '/PING/{print $2}') 
        if [ $? -eq 0 ]; then  
            echo "$ip is up";  
        else   
            echo "host is down";  
        fi  
        sleep 60  
    

    Here is the output:
    Successful ping (& reply), it responds:

        74.125.226.119 is up
    

    However, upon failure to recieve reply it still responds:

        ping: unknown host google.ccccaa 
        is up
    

    rather than echo "host is down"

    Obviously, I have overlooked something. I hate asking questions like this, and I'm sure the answer is already hiding here somewhere, but again I am at a standstill and cannot find what I am looking for. I'm not even really sure what I am looking for.

    EDIT: Solved! Thank you kindly for the helpful advice!

    Here is the final:

        echo & echo "DOI (Domain):" &&read input    
        output=$(ping -c 1 "$input" 2>/dev/null)  
        if [ $? -eq 0 ]; then  
           ip=$(printf '%s' "$output" | gawk -F'[()]' '/PING/{print $2}'  2>/dev/null )  
           echo "$input ($ip) is up";  
        else  
           echo "Host not found";  
        fi  
        sleep 60  
    
    • mpy
      mpy about 11 years
      $? returns the exit status of the last command, in your case that of gawk which will exit successful even if ping returns an error.
    • Sergey Vlasov
      Sergey Vlasov about 11 years
      The first ip=$(ping... is not needed.
  • Sergey Vlasov
    Sergey Vlasov about 11 years
    awk here is used to get the IP address from the ping output and print it instead of the host name.
  • mpy
    mpy about 11 years
    @SergeyVlasov: Sure, but I didn't realized that returning the IP is a requirement until I read OP's previous question (superuser.com/q/602674/195224). Btw. nice approach in your answer (+1)!
  • ICE
    ICE about 11 years
    Thank-you all for the advice! everything is working like a charm now XD
  • mpy
    mpy about 11 years
    @Micheal: Fine. But I think (judging from your ,final' in your question) Sergey's answer deserves the ,accepted'. If you also like my answer, you can come back and upvote it, once you have enough rep ;)
  • ICE
    ICE about 11 years
    It won't let me upvote any answers :S alas, I can only accept (some), and comment on others. So I am being sure to thank everyong for now. 15rep?
  • mpy
    mpy about 11 years
    @Micheal: You can only accept one answer which is the solution to your problem or (if that does not apply) helps you most. Upvoting requires at least 15 reputation (but while writing that, you already reached 10 as s/b upvoted this question ;) and we know that and are not angry when a new user won't upvote ;)
  • Lenny
    Lenny over 5 years
    short version... ping -c1 google.com > /dev/null 2> /dev/null && echo "host alive" || echo "host dead"