Search code examples
bashconnection

Testing computer connectivity with Bash


Being a noob, I can't comment, so posting a question to expand on the answers for this thread; How to test an Internet connection with bash?

for Mac users.

The -w1 or -w 1 option specified in the answers does not work on Mac However, -W1 or -W 1 does work. It is a case sensitivity issue for the Mac rather than an invalid/unavailable option.


Solution

  • On Mac: I assume you need a timeout? What's wrong with using the -t option (timeout) ?

    A script for this, adapted from your link:

    checkInternet () {
        error=$(ping -q  -t1 -c1 "8.8.8.8" 2>&1 >"/dev/null" || true)
    
        if [ "${error}" != "" ]; then
            echo "No internet" >&2
            exit 1
        fi
        echo "Internet" >&2
    }
    
    checkInternet