Search code examples
linuxbashwifiping

How can I check Internet access using a Bash script on Linux?


In my school, the Internet is not available (every night after 23:00 the school will kill the Internet connection, to put us to bed >..<), Then the ping will never stop, though I have used the parameter ping -w1 ....

That is, when I use: ping -q -w1 -c1 8.8.8.8 to check if the Internet connection is up/down, it will be there without any output and doesn't exit, just like I am using a single cat.

I don't know why it's like this, But I think the problem is related to the school-internet-service. Any suggestion? (I think wget may be a good alternative, but how can I use it?)


Solution

  • Using wget:

    #!/bin/bash
        
    wget -q --tries=10 --timeout=20 --spider http://google.com
    if [[ $? -eq 0 ]]; then
        echo "Online"
    else
        echo "Offline"
    fi