I am trying to write a script that lists all the hosts on my LAN (there a about 20 of them) and writes the ping status next to each host. I have the DHCP leases file, so I have all the IPs (say, 10.0.0.1, 10.0.0.2, etc.), all I need is the ping status for each host.
So, my script launches a single ping for each host:
ping -c 1 10.0.0.1
Unfortunately, when a host is offline, the ping takes a long time to timeout. I checked man ping
, there seem to be two options to set the timeout delay: -w deadline
and -W timeout
. I think I'm interested in the latter.
So I tried this:
ping -c 1 -W 1 10.0.0.1
But waiting one second per offline host is still too long. I tried to set it to below a second, but it does not seem to take the parameter into account at all:
ping -c 1 -W 0.1 10.0.0.1 # timeout option is ignored, apparently
Is there a way to set the timeout to a lower value? If not, are there any alternatives?
Edit
- The O.S. is Debian Lenny.
- The hosts I am trying to ping are actually access points. They are on the same vlan and subnet as the users (for simplicity of deployment and replacement). This is why I do not want to scan all the subnet (with a
ping -b
for example).
Edit #2
I accepted the fping
solution (thanks for all the other answers). This command does exactly what I was looking for:
fping -c1 -t500 10.0.0.1 10.0.0.2 10.0.0.3 10.0.0.4
This command takes at most 500ms to complete, and gives me the ping status of all the hosts at once:
10.0.0.1 : [0], 84 bytes, 5.71 ms (5.71 avg, 0% loss)
10.0.0.2 : [0], 84 bytes, 7.95 ms (7.95 avg, 0% loss)
10.0.0.3 : [0], 84 bytes, 16.1 ms (16.1 avg, 0% loss)
10.0.0.4 : [0], 84 bytes, 48.0 ms (48.0 avg, 0% loss)
10.0.0.1 : xmt/rcv/%loss = 1/1/0%, min/avg/max = 5.71/5.71/5.71
10.0.0.2 : xmt/rcv/%loss = 1/1/0%, min/avg/max = 7.95/7.95/7.95
10.0.0.3 : xmt/rcv/%loss = 1/1/0%, min/avg/max = 16.1/16.1/16.1
10.0.0.4 : xmt/rcv/%loss = 1/1/0%, min/avg/max = 48.0/48.0/48.0
On Debian Lenny, installation is trivial:
aptitude update
aptitude install fping
For people looking for a solution that involves
ping
, use the-i
switch.Or, if you want to use 0.1, you'll need to run it as root
No need to download extra utilities.
fping might be a better tool than the stock ping you are using. What OS are you on?
Example:
-c
Number of request packets to send to each target.-t
Initial target timeout in millisecondsYou can set a short timeout with the
timeout
command on Ubuntu/Debian:I would be using nmap for this task.
nmap -sP --max-retries=1 --host-timeout=1500ms 10.0.0.1
See the nmap Documentation for more details on this.
Use -w switch, both on Windows and Debian.
It's a quick way to check if machine responds at all, assuming it'll respond in time less then specified amount of seconds.
you may want to look at arp ping tool if all your hosts are on the physical LAN.It does the same thing but uses layer 2 arp packets to do the 'ping'. You can use a combination of arpping and icmp ping, or in fact tcp ping, to gather what the failure is. EXample is a tcp stack crash, although rare these days, we could find if a machine tcp stack had crashed, as the machine would not respond to ping, however it would respond to arp (which is a different piece of code on the host).
using a combination of arpping, tcpping, and icmp ping you can find out if the service on the machine has crashed, the tcp stack has crashed, or the machine has locked up completely. If you have managed ethernet switches you can get physical link data, revealing if the machine is actually switched on, or had been physically unplugged. We had a situation where machines (clients in public rooms) would be switched off, we gathered that data and the sent wake on lan packets, to power up the machines. :-)
What ever solutions you build, if your network is busy, think about implementiing some kind of qos , so that your monitoring packets take priority on the network, losing measurement packets due to network congestion can give false alarms. If do use qos for monitoring packets, then you'll need to think about gathering data about network utilisation.
So, you can make your monitoring solution as complex or as easy as you like. We find even the most elementary monitoring system is a step in the right direction, at least some administrator is keeping any eye on machines :-).
good luck!
@jordon-bedwell has a great suggestion.
@laszlo-valko https://stackoverflow.com/questions/20359487/why-does-ping-not-timeout-in-linux explains that ping timeouts only start after the ip address has been determined. If you use a dns and your workstation is offline then ping cannot determine the ip address and so appears to wait approximately a default 20+secs before returning false.
Using the linux 'timeout' tool provides more control when running ping with a domain name.
Thanks Guys
If you can scan your subnet (or part of it) without triggering security alarms, and don't mind a bit of extra data, Angry IP Scanner is fast, free, lets you click to sort by status, and can provide more detailed information.
Why not launch ping in the background, with output to a temporary file, in parallel for every host? Then sleep for one second, kill any ping processes that are still running, and read the files to collect the output.
The timeout is an integer value indicating how far and long the packet can be sent. Values below 1 are meaningless. A value of 1 indicates you are pinging immediate neighbors only.
The only way to speed things up is to run a background check and harvest the results. This is what tools like Nagios do.