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 Network Newbie 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 Network Newbie 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 Network Newbie Member

    Thanks...that worked!
     
  19. kielan

    kielan Network Newbie Member

    Another question...I am trying to work out an eventual setup where I can monitor traffic through the router
    and run continuous pings. So I would have a complete picture as to traffic load and ping times.
    My idea is to use iftop as follows : iftop -i vlan2 -t >> iftop1.txt.
    I'm thinking of running this for say 12 hours or so at a time.
    How would I timestamp the output so I can see exactly what the traffic load was at a specific time.
    Does the iftop.txt reside on the router ROM storage or on the usb drive where I have Entware installed?
    I'm worried file might grow too big and crash router!
    In attachment is picture of Tomato's own ip traffic monitor..this is basically what I would like achieve
    but saved to a file on router and timestamped.
     

    Attached Files:

  20. Sean B.

    Sean B. Network Guru Member

    I don't know the output of iftop well enough to make it an example, but here's one with ping:

    Code:
    ping www.google.com | ts >> file.txt
    The file will be created in whatever directory you ran the command in unless you provide a filepath. IE:

    Code:
    ping www.google.com | ts >> /tmp/mnt/flashdrive/file.txt
     
  21. eibgrad

    eibgrad Network Guru Member

    ^^^ What is ts? Did you mean tee?
     
  22. Sean B.

    Sean B. Network Guru Member

  23. eibgrad

    eibgrad Network Guru Member

    Hmm, never seen this in a tomato build. Can't find it in Entware either. Is it buried in some package?
     
  24. Sean B.

    Sean B. Network Guru Member

    Code:
    root@Storage:/tmp/home/root# ts -v
    Unknown option: v
    usage: ts [-r] [-i | -s] [format]
    root@Storage:/tmp/home/root# ts
    HI
    May 10 18:16:04 HI
    TIMESTAMP ME
    May 10 18:16:07 TIMESTAMP ME
    ^C
    root@Storage:/tmp/home/root#
    I believe it's with one of the standard utils packages.
     
    eibgrad likes this.
  25. Sean B.

    Sean B. Network Guru Member

    Code:
    Package: moreutils
    Version: 0.57-1
    Depends: uclibc-opt, perl
    Status: install user installed
    Architecture: arm
    Installed-Time: 1514536474
    
    root@Storage:/tmp/home/root#
    
     
    eibgrad likes this.
  26. eibgrad

    eibgrad Network Guru Member

    Thanks, I appreciate it.
     
  27. kielan

    kielan Network Newbie Member

    Thanks a lot. I have a feeling that it is only going to do a single timestamp at the start of the iftop output
    and not the subsequent readings but I'll give it a try.
    How would I download the package. Do I look for an Ubuntu one.
    I assume wget ..something?
     
  28. Sean B.

    Sean B. Network Guru Member

    ts will put a timestamp in front of every newline input. So as I said, I don't know the output of iftop, but if it logs in a newline update format, it will work. Example output file using ping:

    Code:
    May 10 21:29:00 PING 192.168.1.1 (192.168.1.1): 56 data bytes
    May 10 21:29:00 64 bytes from 192.168.1.1: seq=0 ttl=64 time=0.212 ms
    May 10 21:29:01 64 bytes from 192.168.1.1: seq=1 ttl=64 time=0.137 ms
    May 10 21:29:02 64 bytes from 192.168.1.1: seq=2 ttl=64 time=0.143 ms
    May 10 21:29:03 64 bytes from 192.168.1.1: seq=3 ttl=64 time=0.132 ms
    May 10 21:29:04 64 bytes from 192.168.1.1: seq=4 ttl=64 time=0.140 ms
    May 10 21:29:04
    May 10 21:29:04 --- 192.168.1.1 ping statistics ---
    May 10 21:29:04 5 packets transmitted, 5 packets received, 0% packet loss
    May 10 21:29:04 round-trip min/avg/max = 0.132/0.152/0.212 ms
    You need to run optware/entware and install the utils package from the repo.
     
    Last edited: May 11, 2019
  29. kielan

    kielan Network Newbie Member

    Thanks for your help...much appreciated.
    From what I see..moreutils is not part of Entware's packages...which I do have installed.
    So I've tried downloading with wget from https://launchpad.net/ubuntu/+source/moreutils/0.57-1 but I get this error :

    root@unknown:/tmp/home/root# wget https://launchpad.net/ubuntu/+archi...s/moreutils/0.57-1/moreutils_0.57.orig.tar.gz
    Connecting to launchpad.net (91.189.89.223:443)
    wget: error getting response: Connection reset by peer

    From trawling thru a whole lot of websites this is what I've come across...not sure if this is what's happening in my case (https://www.linksysinfo.org/index.php?threads/tomatousb-wget-command-broken-for-https-sites.72652/) :

    "Today I did analysis on why the Busybox wget command doesn't work with HTTPS sites. @shibby20 @Toastman@Jacky444

    The short of it: Busybox wget relies on openssl s_client for HTTPS (SSL) to work. openssl s_client no longer works in TomatoUSB -- it isn't compiled in."

    Is there another way to download the moreutils package? Or should i find a way to update my wget file?
     
  30. kielan

    kielan Network Newbie Member

    Btw...how do I put my code in those separate boxes ...:)..
     
  31. Sean B.

    Sean B. Network Guru Member

    Download the package here.

    As for wget, is openssl not in the Entware repo either?
     
  32. kielan

    kielan Network Newbie Member

    I think moreutils is not available for Entware..I only see it for Optware...as in your link.
     
  33. Sean B.

    Sean B. Network Guru Member

    Doesn't Entware use a buildroot-uClibc library base as well? If a library version is different just add it. Or clone the moreutils git here and compile. The armel binaries here may work as well.
     
  34. kielan

    kielan Network Newbie Member

    Thanks a lot. Will look into this when I have a bit of time
     
  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