How can I set a short timeout with the ping command?

Solution 1:

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

Solution 2:

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.

Solution 3:

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

timeout 0.2 ping -c1 fqdn || { do_work }

Solution 4:

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.

Solution 5:

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