Hide

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again.

Delete that bloated snippets file you've been using and share your personal repository with the world. 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.


If you have a new feature suggestion or find a bug, please get in touch via http://commandlinefu.uservoice.com/

Get involved!

You can sign-in using OpenID credentials, or register a traditional username and password.

First-time OpenID users will be automatically assigned a username which can be changed after signing in.

Hide

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.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

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:

Hide

News

2011-03-12 - Confoo 2011 presentation
Slides are available from the commandlinefu presentation at Confoo 2011: http://presentations.codeinthehole.com/confoo2011/
2011-01-04 - Moderation now required for new commands
To try and put and end to the spamming, new commands require moderation before they will appear on the site.
2010-12-27 - Apologies for not banning the trolls sooner
Have been away from the interwebs over Christmas. Will be more vigilant henceforth.
2010-09-24 - OAuth and pagination problems fixed
Apologies for the delay in getting Twitter's OAuth supported. Annoying pagination gremlin also fixed.
Hide

Tags

Hide

Functions

Check if a machine is online

Terminal - Check if a machine is online
ping -c 1 -q MACHINE_IP_OR_NAME >/dev/null 2>&1 && echo ONLINE || echo OFFLINE
2012-02-09 06:30:55
User: UnixNeko
Functions: echo ping
9
Check if a machine is online

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).

Alternatives

There are 2 alternatives - vote for the best!

Terminal - Alternatives
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"
2012-02-09 07:00:03
User: crlf
Functions: echo ping read
Tags: bash echo IP ping
1

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 http://www.commandlinefu.com/commands/view/10220/check-if-a-machine-is-online. It is as per the enhancements suggested.

ping1 IPaddr_or_hostname
2012-02-09 17:26:32
User: waibati
Tags: echo IP ping
0

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

Know a better way?

If you can do better, submit your command here.

What others think

The usage of && and || is wrong!

Try:

true && false || echo not true

It will print "not true" because of command `false'!

Comment by uzsolt 131 weeks and 6 days 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"

Comment by crlf 131 weeks and 6 days ago

You didn't even test this before you posted it, did you?

ping -c -q 127.0.0.1 >/dev/null 2>&1 && echo online || echo offline

... results in "offline", despite using 127.0.0.1 which would always be online.

Comment by Mozai 131 weeks and 1 day ago

ah, wait, my test had a typo in it. I typed "-c " instead of "-c 1 "

Comment by Mozai 131 weeks and 1 day ago

@uzsolt Try

false && echo true || echo false

and

true && echo true || echo false
Comment by Mozai 131 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.

Comment by mpb 130 weeks and 6 days 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.

Comment by UnixNeko 123 weeks and 5 days ago

Your point of view

You must be signed in to comment.

Related sites and podcasts