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 -bfor 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
timeout, you can dotimeout 0.1 ping -c 1 ${hostname}. The older versions oftimeoutcommand only support positive integer timeout values. – Trevor Boyd Smith Dec 08 '16 at 14:03pingwill wait for a response - rather it just sends consecutive pings quicker.... – Mtl Dev Oct 11 '18 at 19:28-iswitch). Unfortunately, I realized this way too late. :( Sorry... If you'd make an edit to the answer (any edit), I could rectify my mistake and award you some points back. – MBender Jul 16 '20 at 11:27pingtakes longer. usetimeout 0.1 pig -c1 $hostinstead (assuming linux) – AK_ Nov 05 '20 at 22:37