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/
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.
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
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:
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).
There are 2 alternatives - vote for the best!
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.
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
If you can do better, submit your command here.
You must be signed in to comment.
The usage of && and || is wrong!
Try:
true && false || echo not trueIt will print "not true" because of command `false'!
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"
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.
ah, wait, my test had a typo in it. I typed "-c " instead of "-c 1 "
@uzsolt Try
false && echo true || echo falseand
true && echo true || echo falseOK, 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.
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.