68

How can I ping a certain address and when found, stop pinging.

I want to use it in a bash script, so when the host is starting up, the script keeps on pinging and from the moment the host is available, the script continues...

19 Answers19

110

A further simplification of Martynas' answer:

until ping -c1 www.google.com >/dev/null 2>&1; do :; done

note that ping itself is used as the loop test; as soon as it succeeds, the loop ends. The loop body is empty, with the null command ":" used to prevent a syntax error.

Update: I thought of a way to make Control-C exit the ping loop cleanly. This will run the loop in the background, trap the interrupt (Control-C) signal, and kill the background loop if it occurs:

ping_cancelled=false    # Keep track of whether the loop was cancelled, or succeeded
until ping -c1 "$1" >/dev/null 2>&1; do :; done &    # The "&" backgrounds it
trap "kill $!; ping_cancelled=true" SIGINT
wait $!          # Wait for the loop to exit, one way or another
trap - SIGINT    # Remove the trap, now we're done with it
echo "Done pinging, cancelled=$ping_cancelled"

It's a bit circuitous, but if you want the loop to be cancellable it should do the trick.

44

I know the question is old... and specifically asks regarding ping, but I wanted to share my solution.

I use this when rebooting hosts to know when I can SSH back into them again. (Since ping will respond for several seconds before sshd is started.)

until nc -vzw 2 $host 22; do sleep 2; done
Aaron Copley
  • 12,954
30

You can do a loop, send one ping and depending on the status break the loop, for example (bash):

while true; do ping -c1 www.google.com > /dev/null && break; done

Putting this somewhere in your script will block, until www.google.com is pingable.

squillman
  • 38,163
Martynas
  • 1,287
11

Ping the target host once. Check if the ping succeeded (return value of ping is zero). If host is not alive, ping again.

The following code can be saved as a file and called with the hostname as argument, or stripped of the first and last line and used as function within an existing script (waitForHost hostname).

The code does not evaluate the cause for failure if the ping does not result in a response, thus looping forever if the host does not exist. My BSD manpage lists the meaning of each return value, while the linux one does not, so I guess this might not be portable, that's why I left it out.

#!/bin/bash

PING=`which ping`

function waitForHost
{
    if [ -n "$1" ]; 
    then
        waitForHost1 $1;
    else
        echo "waitForHost: Hostname argument expected"
    fi
}

function waitForHost1
{
    reachable=0;
    while [ $reachable -eq 0 ];
    do
    $PING -q -c 1 $1
    if [ "$?" -eq 0 ];
    then
        reachable=1
    fi
    done
    sleep 5
}
waitForHost $1
8
UNREACHEABLE=1;
while [ $UNREACHEABLE -ne "0" ]; 
   do ping -q -c 1 HOST &> /dev/null; UNREACHEABLE=$?; sleep 1;
done

You may remove sleep 1, it's only here to prevent any flooding problem in case where the host would be reacheable but ping would not exit with code 0.

radius
  • 9,701
4

Please see good options at stackoverflow. Here is a sample in bash, you will have to loop over the following code until it returns a successfull ping result.


ping -c 1 -t 1 192.168.1.1;
if [ $? -eq 0 ]; then
    echo "192.168.1.1 is up";
else 
    echo "ip is down";
fi

3

any of the above loops can also be used with fping rather than ping which, IMO, is better suited for use in scripts than ping itself. See fping(1) for details.

while ! fping -q $HOSTNAMES ; do :; done

also useful for testing if machines are up before doing something on them. A simple example:

for h in HOST1 HOST2 HOST3 ; do
  if fping -q $h ; then
     echo -n "$h : "
     ssh $h "uname -a"
  fi
done
cas
  • 6,841
3

For macOS users, ping has the option -o specifically for this:

-o      Exit successfully after receiving one reply packet.

So the command is simple:

ping -o www.google.com

It returns 0 when the host has been successfully pinged once.

Melebius
  • 139
1

Folks, yes this is old. A lot of good answers here.

But I just found what I call a beauty! In case someone likes native ping counter:

ping $HOST | sed "/ ms$/ q"

Isn't this fascinating? :)

[p@localhost ~]$ VBoxManage startvm VGTU-2021-LDVM1 --type headless && \
> ping 192.168.10.14 | sed "/ ms$/ q"
Waiting for VM "VGTU-2021-LDVM1" to power on...
VM "VGTU-2021-LDVM1" has been successfully started.
PING 192.168.10.14 (192.168.10.14) 56(84) bytes of data.
From 192.168.10.8 icmp_seq=1 Destination Host Unreachable
From 192.168.10.8 icmp_seq=2 Destination Host Unreachable
  ...
From 192.168.10.8 icmp_seq=35 Destination Host Unreachable
From 192.168.10.8 icmp_seq=36 Destination Host Unreachable
64 bytes from 192.168.10.14: icmp_seq=37 ttl=64 time=2003 ms
saulius2
  • 266
1

This will try a given number of times.

t=4; c=0; r=0; until ping -c 1 hostname.com >/dev/null 2>&1 || ((++c >= t)); do r=$?; done; echo $r

Instead of echoing $r, you can test it and act according to its value:

if ((r)); then echo 'Failed to contact host'; else echo 'Continuing with script'; fi
1

In general I want to wait for my database or other server to come up, but I don't want to wait too long. The following code waits for 10 seconds, then sets the exit code if the server did not appear within the time limit.

If the server appears before the time limit, the loop will be short-circuited so the next bit of code can run.

for i in `seq 1 10`; do date ; sleep 1 ; ping -c1 ${HOST} &>/dev/null && break ; done
1

To nicely handle the SIGINT on BSD ping.

HOST=google.com NO=1; while [ $NO -ne 0 ]; do ping -W1 -c1 $HOST &>/dev/null; NO=$?;echo "$(date) ($HOST) $NO" ; done; echo "$(date) ($HOST) reachable"

as a function

ping_until(){
  local NO=1
  while [ $NO -ne 0 ]; do
    ping -W1 -c1 $1 &>/dev/null; NO=$?
    # Optionally block ICMP flooding
    # sleep 1
    echo "$(date) ($1) ($NO)"
  done
}
0
$ cat bin/check_host_up.sh
HOST=$1
PINGCMD="ping -c1 $HOST >/dev/null 2>&1"
while true; do
    eval $PINGCMD && sleep 1 && echo -n . ||    \
    (                                           \
        echo && echo "(${HOST}) down:" $(date) &&         \
            until eval $PINGCMD; do :; done &&  \
        echo "(${HOST}) up  :" $(date)
    );
done
mungayree
  • 101
0

ping $HOSTNAME | grep from -m 1

0

grep works on lines, so:

ping google.com | grep "not"

0

Here's a solution that doesn't require cobbling together a shell script:

watch -n 1 -g -- ping -W 1 -c 1 192.0.2.1

Every second, watch will run ping, which will sent one ICMP echo request to 192.0.2.1, giving up if it does not recieve a response within one second.

watch will exit when the output of that command changes, i.e., when the host becomes available.

0

I took the answer of Gordon Davisson, and packed the solution into a CLI-like application using argc. Now, one can also configure a timeout. Type ./script.sh --help for guidance.

#!/usr/bin/env bash

The following @-annotations belong to https://github.com/sigoden/argc

@describe

Waits until a host becomes available by checking if it replies to ping

messages. The difference to ping -W is that this doesn't send a single

network request with a long wait time, but that it sends many requests. This

is especially helpful when one waits for hosts to come online again, such as

when starting a VM or live-migrating a VM to another host.

@arg host!

@option --timeout=60 Timeout in seconds

@option --request-delay=0.05 Delay between requests in seconds.

set -euo pipefail

argc CLI magic

eval "$(argc --argc-eval "$0" "$@")" # Must appear after all "@cmd" directives!

ping_cancelled=false

seconds since UNIX epoch

timestamp_begin=$(date +%s)

echo "Waiting for host to become available" until ping -c 1 -W 0.05 "$argc_host" >/dev/null 2>&1; do DIFF=$(($(date +%s) - $timestamp_begin)) if [ $DIFF -ge $argc_timeout ]; then echo "Timeout!" exit 1 fi done & # The "&" backgrounds it trap "kill $!; ping_cancelled=true" SIGINT wait $! # Wait for the loop to exit, one way or another trap - SIGINT # Remove the trap, now we're done with it echo "Host is available"

phip1611
  • 111
0

I've been using the following function. I like it because I can tell it to stop trying after a while:

#!/usr/bin/env bash

function networkup {
  # Initialize number of attempts
  reachable=$1
  while [ $reachable -ne 0 ]; do
    # Ping supplied host
    ping -q -c 1 -W 1 "$2" > /dev/null 2>&1
    # Check return code
    if [ $? -eq 0 ]; then
      # Success, we can exit with the right return code
      echo 0
      return
    fi
    # Network down, decrement counter and try again
    let reachable-=1
    # Sleep for one second
    sleep 1
  done
  # Network down, number of attempts exhausted, quiting
  echo 1
}

It can be used like this to launch something:

# Start-up a web browser, if network is up
if [ $(networkup 60 www.google.com) -eq 0 ]; then
  firefox &
fi
oneself
  • 397
0

A further enhancement to Gordon Davisson's answer:

until $(ping -c1 www.google.com &>/dev/null); do :; done

with the surrounding '$()' a subshell is started and therefore you can use Control-C to terminate the loop in the event that the host does not become available.