Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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  
share|improve this question
1  
$? returns the exit status of the last command, in your case that of gawk which will exit successful even if ping returns an error. –  mpy Jun 2 '13 at 13:19
1  
The first ip=$(ping... is not needed. –  Sergey Vlasov Jun 2 '13 at 13:51
1  
Thanks! (I'm still fairly new to linux) –  ICE Jun 2 '13 at 13:54

2 Answers 2

up vote 4 down vote accepted

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
share|improve this answer
    
Thank you very much, –  ICE Jun 2 '13 at 14:04

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|improve this answer
    
awk here is used to get the IP address from the ping output and print it instead of the host name. –  Sergey Vlasov Jun 2 '13 at 13:23
    
@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)! –  mpy Jun 2 '13 at 13:28
1  
Thank-you all for the advice! everything is working like a charm now XD –  ICE Jun 2 '13 at 13:49
1  
@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 ;) –  mpy Jun 2 '13 at 13:56
1  
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? –  ICE Jun 2 '13 at 14:02

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.