Find the answer to your Linux question:
Results 1 to 2 of 2
Long time listener, first time poster.... Not really actually, I've been scripting for the whole of a week so forgive any obvious holes in my training. I'm trying to make ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Sep 2003
    Posts
    1

    cron scripting and ping


    Long time listener, first time poster....

    Not really actually, I've been scripting for the whole of a week so forgive any obvious holes in my training.

    I'm trying to make a cron script that will test wether or not I have internet access every 5 minutes or so. I've figured out how the every 5 minutes part works, but I don't know what command to use to check the internet connection status.

    Using a ping command has been suggested to me, but how do I make the output from the ping command into a variable of some sort that I can use with an if statement?

    Hopefully that all made sense....

    Thanks in advance for the help,
    Dewar

  2. #2
    Linux Guru
    Join Date
    Oct 2001
    Location
    Täby, Sweden
    Posts
    7,578
    I've made a similar script on my router, only that it checks for how much has been transferred as well. Anyway, here's my suggestion:
    Code:
    #!/bin/sh
    
    t=$(date +%s)
    ping -c 1 -w 10 <IP of your ISP's gateway>
    r=$?
    t=$&#40;&#40;$&#40;date +%s&#41; - $t&#41;&#41;
    if &#91; $r -ne 0 &#93;; then
        state=DOWN
    else
        if &#91; $t -ge 5 &#93;; then
            state=VERYSLOW
        elif &#91; $t -ge 1 &#93;; then
            state=SLOW
        else
            state=UP
        fi
    fi
    ostate=$&#40;cat /var/run/inet&#41;
    if &#91; "$ostate" != $state &#93;; then
        echo "$&#40;date "+%b %d %H&#58;%M&#58;%S"&#41;&#58; inet status is $state" >>/var/log/inet
        echo $state >/var/run/inet
    fi

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •