77

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
7ochem
  • 280
  • 1
  • 3
  • 12
MiniQuark
  • 3,695
  • 2
  • 20
  • 23

14 Answers14

59

For people looking for a solution that involves ping, use the -i switch.

ping -i 0.2 www.google.com

Or, if you want to use 0.1, you'll need to run it as root

sudo ping -i 0.1 www.google.com

No need to download extra utilities.

  • 6
    +1 from me. This is the first result when googling "ping timeout", and this is exactly what I was looking for. – Steven Jeffries Jul 30 '15 at 21:27
  • @StevenJeffries Same here! – Luc Jul 16 '16 at 13:53
  • If you have a newer version of `timeout`, you can do `timeout 0.1 ping -c 1 ${hostname}`. The older versions of `timeout` command only support positive integer timeout values. – Trevor Boyd Smith Dec 08 '16 at 14:03
  • i just noticed that further down is [a solution by @jordonbedwell](http://serverfault.com/a/733940/82877) that is functionally the same to my comment. when i posted my comment i didn't see the [solution by @jordonwedwell](http://serverfault.com/a/733940/82877). – Trevor Boyd Smith Dec 08 '16 at 15:30
  • 5
    In case of dead host this solution will print its first useful output after about 1 second. This is definitely wrong answer as @MarcelBurkhard already mentioned. – Victor Yarema Oct 07 '17 at 22:14
  • 1
    The problem with this solution is that -i 0.1 will NOT wait 100ms, but longer, for pings that take more than 100ms, thus throwing the timing off. – Llamageddon Mar 09 '18 at 15:07
  • 8
    This answer is incorrect - it doesn't change how long `ping` will wait for a response - rather it just sends consecutive pings quicker.... – Mtl Dev Oct 11 '18 at 19:28
  • I accidentally down-voted this answer, as I didn't realize this is for the Linux ping (on Windows there's no `-i` switch). 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. – Shaamaan Jul 16 '20 at 11:27
  • this is incorrect information because -i doesn't set timeout when `ping` takes longer. use `timeout 0.1 pig -c1 $host` instead (assuming linux) – AK_ Nov 05 '20 at 22:37
  • +1, i agree on this, as long as builtin tools is sufficient, does not need 3rd party tool – Nuha Dzikri Feb 28 '21 at 04:32
56

fping might be a better tool than the stock ping you are using. What OS are you on?

  • "fping differs from ping in that you can specify any number of targets on the command line, or specify a file containing the lists of targets to ping."
  • "Instead of sending to one target until it times out or replies, fping will send out a ping packet and move on to the next target in a round-robin fashion."
  • "Unlike ping, fping is meant to be used in scripts, so its output is designed to be easy to parse."

Example:

fping -c1 -t500 10.0.0.1 10.0.0.2 10.0.0.3 10.0.0.4
  • -c Number of request packets to send to each target.
  • -t Initial target timeout in milliseconds
mgutt
  • 459
  • 6
  • 22
AndyN
  • 1,739
  • 12
  • 14
  • 1
    Works great, thanks, this is what I was looking for. This command does the job fine: fping -c1 -t500 10.0.0.1 10.0.0.2 10.0.0.3 ... The whole thing lasts half a second, and I get the results for all hosts at once. Excellent tool. :-)) – MiniQuark Nov 12 '10 at 22:47
31

You can set a short timeout with the timeout command on Ubuntu/Debian:

timeout 0.2 ping -c1 fqdn || { do_work }
Jordon Bedwell
  • 410
  • 4
  • 6
  • Good answer! I now did: timeout 1 ping -c 1 test.com – vrijdenker Feb 23 '16 at 10:14
  • 1
    My version of timeout only accepts 1 second. `timeout (GNU coreutils) 8.4`. – slm Jul 22 '16 at 20:05
  • i like this solution, it can be applied to all other commands – Dee Aug 09 '17 at 03:20
  • @slm, it is strange that your version doesn't support less than a second. It works for me: `DURATION is a floating point number with an optional suffix: 's' for seconds (the default),`... In my case `man timeout` reports `GNU coreutils 8.26`. BWT, I'm using Ubuntu 17.04. – Victor Yarema Oct 07 '17 at 22:26
  • This is actually the only thing that works if you want do decrease timeouts (host does not respond)... You have to do the math though: timeout must be greater than "-c" * "-i" of the ping command- – Marki May 20 '19 at 17:22
  • this should be the correct answer. – AK_ Nov 05 '20 at 22:38
  • This works on OSX with coreutils from homebrew. – Joshua Goldberg Mar 01 '22 at 15:27
18

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.

murgo
  • 133
  • 7
pacey
  • 3,833
  • 1
  • 15
  • 31
  • Good call.. This is the "Awesome" way to do it :D +1 – Arenstar Nov 10 '10 at 20:37
  • Hi, I just tried this, but I got the following error message: "--host-timeout is specified in milliseconds unless you qualify it by appending 's', 'm', 'h', or 'd'. The value must be greater than 1500 milliseconds". – MiniQuark Nov 11 '10 at 08:46
  • I think the problem is obvious, the timeout can't be shorter than 1500ms. Than you should look for the nmap options regaring parallelism I think. – pacey Nov 11 '10 at 09:08
  • This seems to only send one request. I am looking for continuous fault detection, I got here by Googling "ping millisecond timeout", Does nmap offer infinie-pings like ubuntu ping does? – ThorSummoner Jun 26 '14 at 16:20
  • setting the timeout to something lower than a second will involve using a privileged user as in @victor-bjelkholm answer – thrau Jan 03 '16 at 00:26
10

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.

ping -w 1 192.168.80.105

PING 192.168.80.105 (192.168.80.105) 56(84) bytes of data.

--- 192.168.80.105 ping statistics ---
2 packets transmitted, 0 received, 100% packet loss, time 999ms
NXT
  • 203
  • 2
  • 6
  • For ubuntu is is -W (capital w) `-w reply wait in seconds` And `-W time to wait for response` – M at Sep 25 '21 at 19:01
4

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!

The Unix Janitor
  • 2,388
  • 14
  • 13
3

@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

Digc
  • 31
  • 2
1

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.

Paul
  • 779
  • 1
  • 9
  • 18
0

On macOS, to set the time ping waits for a response form the server before deciding its a non-response (timeout) is -t:

ping -t 1 -c 1 host
motobói
  • 1,571
  • 11
  • 17
0

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.

dfranke
  • 379
  • 1
  • 7
0

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.

BillThor
  • 27,354
  • 3
  • 35
  • 69
  • 2
    I'm sorry, but I really don't think this is true. The man page is pretty clear that -W specifies the "Time to wait for a response, in seconds"; it's not dimensionless, and although ping doesn't honour values below 1 (hence the question) sub-second timeouts are not meaningless. If you'll forgive me, you may be confusing -W with -t, the latter setting the IP TTL (hop count) field, which does behave as you have described, and where values below 1 are indeed meaningless. – MadHatter Nov 11 '10 at 08:56
  • -W is th time the ping process waits for a response, and it may make sense in to set it well below the hop count when hop count is high, which it normally is. By three seconds you are usually getting into retry conditions. Some tools may allow the use of a finer grained timer for waits, but for most uses of ping, seconds are a reasonable unit. – BillThor Nov 12 '10 at 20:25
0

You could try something like this. But it takes 15 minutes to run.

a=258
while [ $a -ge 1 ]
do
    echo "10.0.0.$a"
    sudo ping -i 0.1 -c 1 "10.0.0.$a">>/home/$USER/output.log
   a=`expr $a - 1`
done
cat /home/$USER/output.log|grep -i "icmp_req=1"
cat /dev/null>/home/$USER/output.log
HBruijn
  • 72,524
  • 21
  • 127
  • 192
0

try this:

ping -n 5 1.2.3.4.5 >nul
Michael Hampton
  • 237,123
  • 42
  • 477
  • 940
Micky
  • 11
-1

ping has [-t timeout] and [-W waittime] options so you could do:

ping -c 1 -t 1 -W 1 google.com
Ivelin
  • 107
  • 1