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

Commands tagged ping from sorted by
Terminal - Commands tagged ping - 44 results
Mac OSX: ping -oc 30 8.8.4.4 > /dev/null && say "Google name server is up" || say "This host is down"
2014-08-01 15:44:57
User: raulyca
Functions: ping
Tags: ping beep
4

Audio acknowledgement for host availability.

When running the command from a Linux systems, you can use "festival" or "espeak" instead of "say".

(prefix="10.59.21" && for i in `seq 254`; do (sleep 0.5 && ping -c1 -w1 $prefix.$i &> /dev/null && arp -n | awk ' /'$prefix'.'$i' / { print $1 " " $3 } ') & done; wait)
2014-04-02 11:20:57
User: smoky
Functions: arp awk ping sleep
Tags: ping
1

Waits for all pings to complete and returns ip with mac address

fping -ga 192.168.1.0/24 2> /dev/null
2014-01-31 19:19:19
User: netaxiz
Tags: nmap ping
2

Will report back IP address's of all hosts that are UP.

nmap -sn 192.168.1.0/24
nmap -sP 192.168.0.1-254
ping -b 192.168.0.255
2014-01-27 08:06:07
User: betsubetsu
Functions: ping
Tags: ping
0

-b option takes the broadcast address.

telnet 0 <port>
2012-11-16 03:38:49
User: somaddict
Functions: telnet
0

Super fast way to ftp/telnet/netcat/ssh/ping your loopback address for testing. The default route 0.0.0.0 is simply reduced to 0.

for host in $HOSTNAMES; do ping -q -c3 $host && ssh $host 'command' & for count in {1..15}; do sleep 1; jobs | wc -l | grep -q ^0\$ && continue; done; kill %1; done &>/dev/null
for host in $MYHOSTS; do ping -q -c3 $H 2>&1 1>/dev/null && ssh -o 'AllowedAuthe ntications publickey' $host 'command1; command2' & for count in 1 2 3 4 5; do sleep 1; jobs | wc -l | grep -q ^0\$ && continue; done; kill %1; done
2012-11-13 23:12:27
User: a8ksh4
Functions: grep host jobs kill ping sleep ssh wc
0

Execute commands serially on a list of hosts. Each ssh connection is made in the background so that if, after five seconds, it hasn't closed, it will be killed and the script will go on to the next system.

Maybe there's an easier way to set a timeout in the ssh options...

prefix="169.254" && for i in {0..254}; do echo $prefix.$i/8; for j in {1..254}; do sh -c "ping -m 1 -c 1 -t 1 $prefix.$i.$j | grep \"icmp\" &" ; done; done
2012-07-25 12:07:15
User: tempelorg
Functions: echo sh
Tags: ping
0

This version combines the best of the other suggestions and adds these features:

1. It scans a /16 subnet

2. It is very fast by running the ping commands in the background, running them in parallel.

3. Does not use the "-W" option as that's not available in older ping versions (I needed this for OS X 10.5)

ping g.co|perl -ne'$|=/e=(\S+)/||next;(push@_,$1)>30&&shift@_;print"\r",(map{"\xe2\x96".chr(128+7*$_/(sort{$b<=>$a}@_)[0])." "}@_),"$1ms"'
2012-07-06 22:42:06
User: bartgrantham
Functions: perl ping
1

Nasty perl one-liner that provides a sparkline of ping times. If you want a different history than the last 30, just put that value in. It (ab)uses unicode to draw the bars, inspired by https://github.com/joemiller/spark-ping . It's not the most bug-free piece of code, but what it lacks in robustness it makes up for in capability. :)

If anyone has any ideas on how to make it more compact or better, I'd love to hear them.

I included a ping to google in the command just as an example (and burned up 10 chars doing it!). You should use it with: $ ping example.com | $SPARKLINE_PING_COMMAND

ping -b <broadcast address>
2012-05-21 16:55:33
User: OutputLogic
Functions: ping
Tags: ping broadcast
-1

A simple way to find all machines on a network subnet is pinging a broadcast address (-b flag). First run ifconfig ifconfig. Then use "Bcast" address and '-b' flag in ping

mtr google.com
2012-02-19 22:27:48
User: d_voge
-1

You need to have mtr installed on your host.

for i in {1..30}; do ping -t $i -c 1 google.com; done | grep "Time to live exceeded"
2012-02-19 13:37:04
User: fossilet
Functions: grep ping
12

This command uses ping to get the routers' IP addresses to the destination host as traceroute does. If you know what I mean..

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

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.

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
Tags: echo IP ping
9

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

10,30,50 * * * * ping -c1 -w3 192.168.0.14 >/dev/null
2012-02-06 14:54:15
User: bashrc
Functions: ping
2

Cleaner with a mailto assignment in crontab (if the command fails you get an email):

MAILTO=admin@example.com

10,30,50 * * * * ping -q -c1 -w3 192.168.0.14 >/dev/null

10,30,50 * * * * ping -q -c1 -w3 192.168.0.14 | grep '1 received' - || mail -ne -s'Host 192.168.0.14 not reachable' admin@example.com
2012-02-06 10:42:46
User: knoppix5
Functions: grep mail ping
-4

Every 20 minutes test if host with IP 192.168.0.14 is 'dead' or not reachable.

The line should be put in your crontab file.

10,30,50 * * * * ping -q -c1 -w3 192.168.0.14 | grep '1 received' - || env DISPLAY=:0 xeyes
2012-02-06 09:40:11
User: knoppix5
Functions: env grep ping
Tags: ping host crontab
-2

Every 20 minutes ping host with IP address 192.168.0.14. If it's not 'alive' or not reachable, then display something eye-catching (here xeyes) on the desktop.

Hint for newbies: edit crontab with

crontab -e
ping -qc 10 server.tld | awk -F/ '/^rtt/ {print $5}'
2011-10-12 21:07:06
User: atoponce
Functions: awk ping
Tags: awk ping
-4

Quick and dirty one-liner to get the average ping(1) time from a server.

ping HOSTNAME | while read pong; do echo "$(date): $pong"; done
nmap -sT -PN -vv <target ip>
2011-07-22 02:37:19
User: Richie086
0

Change the IP address from 127.0.0.1 to the target machines ip address. Even if the target has ICMP (ping) blocked, it will show you what ports are open on the target. Very handy for situations where you know the target is up and online but wont respond to pings.

while true; do /bin/ping -q -c1 -w3 8.8.8.8 2>&1 > /dev/null || echo "8.8.8.8 ping failed at $(date +%d/%m/%y) $(date +%H:%M:%S)" >> /var/log/ping.log; sleep 10; done &
2011-05-12 01:51:07
User: Arkey
Functions: echo sleep
-1

This command are using by me for checking connection with Internet.

ping -a IP-ADDRESS
2011-04-28 13:51:12
User: markussesser
Functions: ping
0

pcspkr have to be enabled!

modprobe pcspkr

xset b on