How to check internet access using bash script in linux?

眉间皱痕 提交于 2019-12-03 05:04:27

问题


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?)


回答1:


Using wget:

#!/bin/bash

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



回答2:


If the school actually turns off their router instead of redirecting all traffic to a "why aren't you in bed" page, then there's no need to download an entire web page or send HTTP headers. All you have to do is just make a connection and check if someone's listening.

nc -z 8.8.8.8 53

This will output "Connection to 8.8.8.8 port 53 [tcp/domain] succeeded!" and return a value of 0 if someone's listening.

If you want to use it in a shell script:

nc -z 8.8.8.8 53  >/dev/null 2>&1
online=$?
if [ $online -eq 0 ]; then
    echo "Online"
else
    echo "Offline"
fi



回答3:


#!/bin/bash

INTERNET_STATUS="UNKNOWN"
TIMESTAMP=`date +%s`
while [ 1 ]
 do
    ping -c 1 -W 0.7 8.8.4.4 > /dev/null 2>&1
    if [ $? -eq 0 ] ; then
        if [ "$INTERNET_STATUS" != "UP" ]; then
            echo "UP   `date +%Y-%m-%dT%H:%M:%S%Z` $((`date +%s`-$TIMESTAMP))";
            INTERNET_STATUS="UP"
        fi
    else
        if [ "$INTERNET_STATUS" = "UP" ]; then
            echo "DOWN `date +%Y-%m-%dT%H:%M:%S%Z` $((`date +%s`-$TIMESTAMP))";
            INTERNET_STATUS="DOWN"
        fi
    fi
    sleep 1
 done;

the output will produce smth like:

bash-3.2$ ./internet_check.sh
UP   2016-05-10T23:23:06BST 4
DOWN 2016-05-10T23:23:25BST 19
UP   2016-05-10T23:23:32BST 7

the number in the end of a line shows duration of previous state, i.e. 19 up, 7 secs down




回答4:


Use the timeout option -t:

ping -q -t 5 -w1 -c1 8.8.8.8 t



回答5:


Install fping: > less problem then ping.

fping google.com | grep alive

to use for example like:

#!/bin/bash

itest=$(fping google.com | grep alive)

while [ "$itest" == "" ] 
        do
        sleep 5
        itest=$(fping google.com | grep alive)
done
echo now online



回答6:


Without wget

#!/bin/bash

echo -e "GET http://google.com HTTP/1.0\n\n" | nc google.com 80 > /dev/null 2>&1

if [ $? -eq 0 ]; then
    echo "Online"
else
    echo "Offline"
fi

Enjoy ;)




回答7:


Using the example above, I wrote this script to log the state of your connection: https://gist.github.com/cganterh/ffc2fffa8263857cbece

First, save the following code into a name.sh file.

#!/bin/bash

while true
do
    wget -q --tries=10 --timeout=20 -O - http://google.com > /dev/null
    if [[ $? -eq 0 ]]; then
        echo $(date) "1" | tee -a log.csv
    else
        echo $(date) "0" | tee -a log.csv
    fi
    sleep 5
done

Then, execute name.sh file in terminal, then check the log state information in log.csv of the same folder.




回答8:


Reliable old ping in a separate bash script:

#!/bin/bash
ipaddr='8.8.8.8' # Google's public DNS server
[[ -z `ping -c1 $ipaddr |& grep -o 'Network is unreachable'` ]] || exit 1
[[ -z `ping -c3 $ipaddr |& grep -o '100% packet loss'` ]] && exit 0 || exit 1

Put this on a separate script. It will handle different network situations as (1) not being connected to a network, (2) connected to the network but cannot access the internet (or at least Google), and (3) connected to the internet.


You may later use the exit code of the script to check connectivity, e.g.

~$ script-name && echo online || echo offline



回答9:


I decided to combine a few of the above so I could later create a plot showing ups, downs, and their durations:

#!/bin/bash
#
# pinger is a bash shell script that monitors the network 
# status every 15 seconds and records if it is up '1' or down '0'
# into the file log.csv from whence it may be plotted.
#
# author: J. W. Wooten, Ph.D.
# since: 11/12/2019
# version: 1.0
#
TIMESTAMP=`date +%s`
while [ 1 ]
  do
    nc -z -w 5 8.8.8.8 53  >/dev/null 2>&1
online=$?
    TIME=`date +%s`
    if [ $online -eq 0 ]; then
      echo "`date +%Y-%m-%d_%H:%M:%S_%Z` 1 $(($TIME-$TIMESTAMP))" | tee -a log.csv
    else
      echo "`date +%Y-%m-%d_%H:%M:%S_%Z` 0 $(($TIME-$TIMESTAMP))" | tee -a log.csv
    fi
    TIMESTAMP=$TIME
    sleep 15
  done;

this outputs to a csv file every 15 seconds. Using Excel or Numbers, you can read the file and create a plot which will show when internet was not available and also the duration. If it changes from your sleep interval, then it is spending time trying to connect. Hope to add the ability to send me a text when it detects network is down next. Thanks to all above.



来源:https://stackoverflow.com/questions/17291233/how-to-check-internet-access-using-bash-script-in-linux

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!