This function I wrote should exemplify the idiosyncrasy between OSX and Linux ping commands, in particular their -W timeout option:

# Checks if a host is up-and-running and responding to pings

function my_function_is_network_host_up() {

  local -ri N_ARGUMENTS=1
  if ! check_n_arguments "${FUNCNAME}" "${N_ARGUMENTS}" "${@}"; then return; fi

  local -r NETWORK_HOST=$1

  local -ri n_requests=1

  local -i wait_for_reply=1 # Seconds
  if [[ $my_global_os_type == 'OSX' ]]; then
    ((wait_for_reply *= 1000)) # Milliseconds

  ping -c "$n_requests" \
       -W "$wait_for_reply" \
       -q \
       "$NETWORK_HOST" \
       &> /dev/null

  return $?


Are real sysadmins using a better Bash function to be able to check hosts independently from the machine they have just hopped onto?

My typical usage:

for h in "${hs[@]}"; do
    if my_function_is_network_host_up $h; then

or just:

my_function_is_network_host_up $h && do_stuff



Time in milliseconds to wait for a reply for each packet sent.  If a reply arrives later, the packet is not printed as replied, but considered as replied when calculating statistics.


Time to wait for a response, in seconds. The option affects only timeout in absense of any responses, otherwise ping waits for two RTTs.
  • JFTR typically you wouldn't use a function for that as the check if a host is up is done by the monitoring system. If you have to do a manual check you just ping and press ctrl+c to cancel. – Ulrich Dangel Apr 1 '13 at 13:52
  • Why would you not use that function? – Robottinosino Apr 1 '13 at 18:11

I don't understand what your function is supposed to do. How is it better than simply running

for host in host1 host2 host3; do ping -c 1 host >/dev/null && do_stuff; done

If all you want to know is if the host is up or not, and run a command if it is, the above should be sufficient.

  • why have that function? 1. no stdout output; 2. precise timeout, indep. from the platform (predictable wait on batch execution) 3. can use auto-completion and is more readable 4. in a script, I can add logging of unavailable hosts by editing a single func def – Robottinosino Apr 1 '13 at 20:05
  • OK, then please explain your requirements so we can understand what you are trying to do. Why is the precise timeout relevant? What autocompletion? How is it more readable? In a script you can add logging of unavailable hosts by adding a host to the hosts array (see updated answer). – terdon Apr 1 '13 at 20:09
  • Precise timeout: so that I know 100 hosts will take at most 100 seconds. Bash command autocompletion: I have my functions all starting with my_function_ so that I can tab-complete them and have the standard options (in this case -c 1) auto-selected. Readable is a matter of personal taste: for functions I define with many options "pre-selected" I find reading the function name more "natural language like" than ping -o1 -o2 -o3 -o4.. – Robottinosino Apr 1 '13 at 20:17
  • Well, sounds to me like you have everything in hand, not the way I would personally have done it but that is a matter of personal preference as you say, I have no reason to thing that my way would be any better. – terdon Apr 1 '13 at 23:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.