Sunday, 2 June 2013

LINUX Ping host, Display error on failure

LINUX Ping host, Display error on failure

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.

No comments:

Post a Comment