wenlibin02 wenlibin02 - 2 months ago 7
Linux Question

How to check internet access using bash script in linux?

In my school, the internet is not available(every night after 23:0 the school will kill the internet, to put us in 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 is up/down, It will be there without any output and doesn't exit, just like I am using a single
cat
.

Can you understand my question??? 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 to use?)

Answer

Using wget:

#!/bin/bash

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