How to check internet access using bash script in linux?

前端 未结 10 1149
孤街浪徒
孤街浪徒 2021-02-01 06:43

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 use

10条回答
  •  有刺的猬
    2021-02-01 07:26

    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.

提交回复
热议问题