Check if a machine is online

ping -c 1 -q MACHINE_IP_OR_NAME >/dev/null 2>&1 && echo ONLINE || echo OFFLINE
PING parameters c 1 limits to 1 pinging attempt q makes the command quiet (or silent mode) /dev/null 2>&1 is to remove the display && echo ONLINE is executed if previous command is successful (return value 0) || echo OFFLINE is executed otherwise (return value of 1 if unreachable or 2 if you're offline yourself). I personally use this command as an alias with a predefined machine name but there are at least 2 improvements that may be done. Asking for the machine name or IP Escaping the output so that it displays ONLINE in green and OFFLINE in red (for instance).

By: UnixNeko
2012-02-09 06:30:55

2 Alternatives + Submit Alt

  • I have used single packet, and in a silent mode with no display of ping stats. This is with color and UI improvement to the It is as per the enhancements suggested. Show Sample Output

    echo -n "IP Address or Machine Name: "; read IP; ping -c 1 -q $IP >/dev/null 2>&1 && echo -e "\e[00;32mOnline\e[00m" || echo -e "\e[00;31mOffline\e[00m"
    crlf · 2012-02-09 07:00:03 1
  • export THISOS="`uname -s`" if [ "$THISOS" = "SunOS" ] then export THISRELEASE="`uname -r`" ping1() { ping -s $1 56 1 | egrep "^64"; } elif [ "$THISOS" = "AIX" ] then export THISRELEASE="`uname -v`.`uname -r`" ping1() { ping -w ${2:-1} $1 56 1 | egrep "^64"; } elif [ "$THISOS" = "Linux" ] then export THISRELEASE="`uname -r`" ping1() { ping -c 1 -w ${2:-1} $1 | egrep "^64"; } fi

    ping1 IPaddr_or_hostname
    waibati · 2012-02-09 17:26:32 0

What Others Think

The usage of && and || is wrong! Try: true && false || echo not true It will print "not true" because of command `false'!
uzsolt · 548 weeks ago
This is the improved version. echo -n "IP Address or Machine Name: "; read IP; ping -c 1 -q $IP >/dev/null 2>&1 && echo -e "\e[00;32mOnline\e[00m" || echo -e "\e[00;31mOffline\e[00m"
crlf · 548 weeks ago
You didn't even test this before you posted it, did you? ping -c -q >/dev/null 2>&1 && echo online || echo offline ... results in "offline", despite using which would always be online.
Mozai · 547 weeks and 1 day ago
ah, wait, my test had a typo in it. I typed "-c " instead of "-c 1 "
Mozai · 547 weeks and 1 day ago
@uzsolt Try false && echo true || echo false and true && echo true || echo false
Mozai · 547 weeks and 1 day ago
OK, so it is a fundamental error to believe that if a system does not respond to ICMP echo request then it is not online. The paranoid amongst us (and those who remember "ping-of-death" attacks) disable ICMP echo request acknowledgement.
mpb · 547 weeks ago
crlf : You should upload that updated one ;) uzsolt : I don't think so. ;) Mozai : tested for sure but you already know it. mpb : true, but my command assumes you don't disable echo. Maybe should I have mentioned this.
UnixNeko · 539 weeks and 5 days ago

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this? is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands

Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.


Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: