commandlinefu.com is the place to record those command-line gems that you return to again and again.
You can sign-in using OpenID credentials, or register a traditional username and password.
Subscribe to the feed for:
Waits for all pings to complete and returns ip with mac address
-b option takes the broadcast address.
Pings all the hosts on 192.168.1.0/24 in parallel, so it is very fast. Alive host IP addresses are echoed to stdout as pings are responded to.
Cleaned up and silent with &>/dev/null at the end.
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...
%t are tens. %d are digits. One may have further outer loops to provide hundreds, thousands, etc.
This example applies ping to the numbered machines. The pattern can be used in other ways to apply all combinations of components to a task.
For less time
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
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
Linux - starting with a packetsize that must be split into two packets, count down by 8 bytes, and try to send the packet using the "Don't Fragment" option. The actual MTU (the size of the actual PING packet) is (in this example) 1460 data bytes + 20 bytes IP header + 8 bytes PING request = 1488
This command uses ping to get the routers' IP addresses to the destination host as traceroute does. If you know what I mean..
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.
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).
Cleaner with a mailto assignment in crontab (if the command fails you get an email):
10,30,50 * * * * ping -q -c1 -w3 192.168.0.14 >/dev/null
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.
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
Quick and dirty one-liner to get the average ping(1) time from a server.
I'd rather this one on Gnome, as I'm used to be listening some music while working. I've even created a bash function which receives ADDRESS as parameter.
In case sed and awk are not available you may use this to remove the last character from a string with "rev" and "cut".