Using ping to check if a host is up works, but it's slower than it needs to be, especially when the host is down. This occurs because ping assumes the network is either unreliable or slow by default. This makes sense to diagnose unreliable connections, but doesn't make sense on a reliable network.

ping on Windows only sends a few packets, then exits. ping on Linux/Mac OS sends packets forever. This also makes ping default behavior undesireable to just check if a host is up. The wait parameter is also different on Linux/Mac OS, hence the need for the wflag function.

Let's change the behavior of ping to just ping twice quickly:

wflag() {
	uname | egrep 'Linux|Darwin' >/dev/null 2>&1 && echo W || echo w

isUp() { 
  ping -nq -c 2 -$(wflag) 2 $1 | grep -q '0.0% packet loss'

isUp localhost && echo localhost is up || echo localhost is down

Using these parameters ping will only run 1 second to determine if the host is up. If the host is down, it will run for 4 seconds. This is reasonable behavior for use in a script for instance.