Script to switch to 4G?

Discussion in 'Tomato Firmware' started by Stuvning, Jun 29, 2018.

  1. Stuvning

    Stuvning New Member Member

    I've been using Tomato on my router for a while and it works great!
    I switch between ADSL DHCP and a 4G connection quite often amd I'm trying to find a way to speed this up. Is it possible to do this via ssh/telnet and automate the switch instead of doing it via the web interface? I would greatly appreciate it if someone could point me in the right direction.

    thanks in advance!
  2. Combat619

    Combat619 Connected Client Member

    Dowload putty or winscp but you need to know the script and commands

    Sent from my SAMSUNG-SM-G920AZ using Tapatalk
  3. Stuvning

    Stuvning New Member Member

    I tried looking for some relevant commands without luck. I'm guessing I'll have to edit the configuration files but I don't know what I need to change?
  4. Sean B.

    Sean B. LI Guru Member

    I have never used the cellular connection ability of Tomato, but if you describe the exact steps you take to change between the two in the GUI I can then look through the code and likely determine a way to script commands for it.
    Malakai likes this.
  5. Stuvning

    Stuvning New Member Member

    The steps are as follows:
    1. Basic -> Network
    2. WAN Settings -> Type
    3. Here I switch between DHCP and 4G/LTE
    4. Save
  6. Sean B.

    Sean B. LI Guru Member

    No usernames or passwords? No other settings/options are different depending on whether DHCP or 4G has been selected?
  7. Stuvning

    Stuvning New Member Member

    I login to the Tomato Web-GUI, as for the setting I've only had to set those up once. They are already filled in when I switch WAN-type. But they are as follows:


    Type: DHCP
    Wireless Client Mode: Disabled
    DNS Server: Auto
    MTU: Default
    Route Modem IP:


    Type: 4G/LTE
    PIN Code: Empty
    Network Type: Auto
    DNS Server: Auto
    MTU: Default
    Last edited: Jul 3, 2018
  8. Sean B.

    Sean B. LI Guru Member

    Give this a try:

    if [ $(nvram get wan_proto) == dhcp ]
      elif [ $(nvram get wan_proto) == lte ]
    if [ $WANSTATE == 1 ]
        logger "WAN connection change initiated. Current connection is DHCP, switching to 4G/LTE."
        nvram set wan_proto=lte
        service wan restart
        exit 0
      elif [ $WANSTATE == 2 ]
          logger "WAN connection change initiated. Current connection is 4G/LTE, switching to DHCP."
          nvram set wan_proto=dhcp
          service wan restart
          exit 0
        logger "WAN connection change initiated. **ERROR**: Current WAN connection type is unknown - aborting."
        exit 1
    Copy that and name the file something like "wanchange", or whatever you like. Transfer it to the router in a manor that will be persistent, such as:
    • If you're running optware/entware, then put it in /opt/sbin <-- Preferred
    • If you have permanent USB storage attached to the router, you can place it there. However, make sure you've configured it so the mount point is always the same.
    • A jffs partition

    For these commands, I'll use /your/path as an example, change accordingly depending on where you put the file.

    Via the router GUI under Tools->System commands, or a telnet/ssh shell in the router run:

    chmod +x /your/path/wanchange
    If you put the file somewhere other than /opt/sbin ( or other pre-existing persistent storage path ), you need to add that path into the nvram env_path variable as well, to avoid having to use the full file path for execution. Run:

    nvram set env_path="$(nvram get env_path)/your/path:"
    nvram commit
    Make sure to put that : at the end.

    Now try running wanchange as a command, and see if it works as expected.

    **NOTE** Be careful with copy/paste, as some systems and text editors can screw up the spacing and/or CF/LR etc. Also, I did not code the script to commit its WAN type changes to nvram. In other words, if the router gets rebooted or loses power for any reason, upon boot the WAN connection type will be whatever you had last selected in the GUI and saved, regardless of what it was after the script has changed it. This saves on writes to the flash memory itself, as flash has a life expectancy measured in write cycles. If you'd prefer each and every type change the script makes to be committed so it is persistent, let me know and I'll amend the code. The script is coded to output info to the system log, but if you'd prefer it to echo the info to the shell instead I can change that as well.
    Last edited: Jul 3, 2018
  9. Stuvning

    Stuvning New Member Member

    Works like an absolute charm! Thank you so much for the help!
  10. Sean B.

    Sean B. LI Guru Member

    You're welcome, happy to help.
  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