Looking for commands or scripts Tomato

Discussion in 'Tomato Firmware' started by Mistek, Dec 27, 2016.

  1. Mistek

    Mistek New Member Member

    I am looking for a solution for my router. firmware works fine. Sometimes my provider does work not fine, and the router loses connection. I have to manually update the connection and then everything works. I need a command or script to automatically update the connection. Suppose a router pings the site every 30 seconds (does not matter which one), and if the ping fails, the router should automatically refresh the connection. Internet works through dynamic address(just DHCP ip). I would be grateful for any hint in the decision.
     
  2. Sean B.

    Sean B. Network Guru Member

    What build are you running?
     
  3. Mistek

    Mistek New Member Member

    Version 1.28 by shibby

    1.28.0000 MIPSR2-132 K26 MiniVPN
     
  4. Sean B.

    Sean B. Network Guru Member

    Put this in one of the schedulers in Administration->scheduler setting it for every day at whatever interval you like. It pings googles DNS server 5 times, if they all fail it will restart your WAN connection and make a note of doing so in your syslog. Try it when you're connected and make sure it doesn't do anything, and then try it after manually releasing your connection and make sure it restarts it properly.

    Code:
    #!/bin/sh
    ping -q -c 5 8.8.8.8 >/dev/null 2>&1;
    STATUS=$?
    
    if [ $STATUS == 1 ]
     then
      service wan restart;
      logger "WAN connection ping test failed, restarting WAN services.";
    fi
    exit 0;
     
    Mistek likes this.
  5. Sean B.

    Sean B. Network Guru Member

    Afterthought: This one will confirm it successfully checked the WAN connection by noting it in your syslog. Will serve to verify functionality when first checking, as this way you can check your syslog after whatever interval you set the scheduler at to confirm. Then if testing shows good, you can replace it with the code from the last post so it will only write to the syslog when a WAN failure occurs.

    Code:
    #!/bin/sh
    ping -q -c 5 8.8.8.8 >/dev/null 2>&1;
    STATUS=$?
    
    if [ $STATUS == 1 ]
     then
      service wan restart;
      logger "WAN connection ping test failed, restarting wan services.";
     else
      logger "WAN connection ping test successful.";
    fi
    exit 0;
     
    Mistek likes this.
  6. Mistek

    Mistek New Member Member

    thank you very much man, it really works) :)
     
  7. Sean B.

    Sean B. Network Guru Member

    Nice. No problem, glad to help :).
     
  8. Sean B.

    Sean B. Network Guru Member

    I should mention to keep in mind this is a very simple script. It does not keep a count of restarts etc, so if even after a WAN restart the connection is still down the script will keep restarting the WAN at every interval it's run. It will only "give up" once the pings succeed. And while googles DNS server is about as reliable as you can get in terms of not being down on their end, if for some reason it is or a routing issue arises in between you and googles DNS server preventing the pings from succeeding it will restart your WAN regardless of your actual connection status.
     
  9. koitsu

    koitsu Network Guru Member

    You don't need semi-colons on the end of your shell script lines, Sean. This is shell, not C. :)

    OP should really have his ISP figure out what's broken. If it's DSL or cable, odds are the actual problem has to do with SNR or dB (signal levels) on the line; a tech can come out and look at+fix that.
     
  10. Sean B.

    Sean B. Network Guru Member

    Good point, been rackin' the brain on C too much lately ;).
     
    AndreDVJ likes this.
  11. kielan

    kielan New Member Member

    Just a question from a beginner regarding ping command as well. I would like to run a remote continuous ping command to check connectivity on my router which is sometimes flaky. I have something like this :
    ping -c 5000 X.X.X.X | while read pong; do echo “$(date): $pong”; done >> pingtest.txt
    I can then check when connection went down or slow pings. But I can't see the result on terminal as well.
    Alternatively I can send to terminal, but then not on file by removing ">>pingtest.txt"
    Is there a way to send to file and to terminal at the same time?
     
  12. eibgrad

    eibgrad Network Guru Member

    A sophisticated approach would be to use the Linux screen command. I use it all the time. Works great. But it requires installation via Entware (additional firmware).

    In fact, here's a script I use sometimes on tomato router's w/ an undependable ISP.

    Code:
    #!/bin/sh
    INTERVAL=20
    WAN_GW=$(nvram get wan_gateway)
    
    ### function wanup()
    wanup() {
        [ "$WAN_GW" == ""              ] && { sleep 10; return 1; }
        [ "$WAN_GW" == "0.0.0.0"       ] && { sleep 10; return 1; }
        [ "$WAN_GW" == "192.168.100.1" ] && { sleep 10; return 1; }
    
        ping -qc1 -w10 "$WAN_GW" > /dev/null 2>&1 "$WAN_GW"
    }
    
    # periodically verify WAN is still connected
    while sleep $INTERVAL; do
        for i in 1 2 3; do
            wanup && { com_echo "Network is up via $WAN_GW"; continue 2; }
        done
        nvram commit
        reboot
    done
    Every 20 secs (adjustable), it verifies the gateway IP is pingable by giving it three attempts. If there's no response, it reboots the router. But I don't just place it in the init script. Instead, I place the script in JFFS (e.g., /jffs/always-on-wan.sh) and add the following to the init script.

    Code:
    screen -dmS wan /jffs/always-on-wan.sh
    (the third argument is just an arbitrary name so you can reference that particular screen buffer; I used "wan")

    Now I can go to a shell/terminal (telnet/ssh) at any time and reattach to the running script to see the output.

    Code:
    screen -r wan
    To detach, you use Ctrl + a (release) then d.

    The script above doesn't write to a file, but it's easy enough to add that feature. Or even write to the syslog.

    Everyone w/ even a modest interest in the Linux shell should learn to use screen. It's incredibly useful. So rather than having to write to a file to check on your background scripts, you can simply reattach yourself to the scripts terminal output at any time.

    Btw, installing Entware on tomato router is very easy; there's already a script installed by the developers for this purpose.

    Code:
    /usr/sbin/entware-install.sh
    Then install the screen package.

    Code:
    opkg install screen
     
  13. Sean B.

    Sean B. Network Guru Member

    Code:
    ping -c 5000 X.X.X.X | while read pong; do echo $(date): $pong; done 2>&1 | tee -a pingtest.txt
    Will output to terminal and append to file.
     
  14. Sean B.

    Sean B. Network Guru Member

    Why reboot the router rather than just call

    Code:
    service wan restart
    ?

    And why the nvram commit with no nvram variable changes?
     
  15. jorkki

    jorkki New Member Member

    I have a nasty 4G operator on a remote site. What they do is they give the WAN IP address with maximum DHCP lease time (around 136 years or so) but every few days they just silently release the IP and Tomato does not survive this as it does not understand what just happened.
    I have solved this by putting a one liner in Administration -> Scheduler to run every hour:

    /bin/ping -c 20 -w 30 8.8.8.8 > /dev/null; if [[ $? != 0 ]]; then service wan restart; /usr/bin/logger "restarted wan service"; fi

    What this does it pings Google expecting 20 replies in 30 seconds. If that fails or some other error occurs the WAN service is restarted and a log entry is created. Sometimes a few pings may get lost so this way unnecessary restarts can be avoided.
     
  16. kielan

    kielan New Member Member

    Thanks guys for the replies...was a bit tied up so only got to it now.
    I've tried the single line entry of Sean B but get the following error...tee: not found...can't understand why it doesn't take it?
    root@unknown:/tmp/home/root# ping -c 5000 8.8.8.8 | while read pong; do echo $(date): $pong; done 2>&1 | tee -a pingtest.txt
    -sh: tee: not found
    I'm using Tomato Shibby v1.28.0000 MIPSR2-140 K26 USB AIO with ENTWare installed.
    I'm busy working my way to understand the other script ..newbie :)...but I don't want to reboot router or restart WAN...I just want to view and store ping results to present to ISP. As mentioned I have Entware installed...so I'll be installing screen command...looks very useful. There seems to be a lot of different ways to get a desired outcome...
     
  17. Sean B.

    Sean B. Network Guru Member

    Install coreutils via entware. It contains tee a long with many other "core" GNU linux utilities.
     
  18. kielan

    kielan New Member Member

    Thanks...that worked!
     
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice