1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

How to control Tomato using 3rd party software?

Discussion in 'Tomato Firmware' started by Telex123, Mar 6, 2012.

  1. Telex123

    Telex123 Networkin' Nut Member

    • Arrive home, disarm alarm system, automatically enable wireless radio.
    • Enable alarm system, leave home, automatically disable wireless radio.
    I'm looking for a way to interface Tomato with my Home Automation system. The simplest way would be via a URL that would look something like: http://routerIP/root:mypassword?enable-script

    Basically, the URL logs in and runs a script. The script would enable/disable the radio and whatever else one would like to do. Is such a thing possible?

    Alternately, the Home Automation software could send a custom command to Tomato via an assigned TCP port (LAN only; not WAN). This is a more complicated arrangement that would require a custom service running on Tomato that monitors the port, interprets the received command, and performs the requested action. I'm not sure if this is feasible.

    Any other ideas on how a Tomato-based router could be controlled by 3rd party software?
  2. Planiwa

    Planiwa LI Guru Member

    This may be much less "complicated" than you imagine. Check out the nc commad.
    (Rodney Hester compiled a real nc: http://multics.minidns.net/tomato/PRECOMPILED-static/ )
  3. Telex123

    Telex123 Networkin' Nut Member

    Thanks for your suggestion. I've started reading about NetCat (nc) and my primary concern is that when you set it to listen on a port, using something like this: nc -l 12345 it will listen on all interfaces including WAN. At least that's my understanding of how it works.
  4. rhester72

    rhester72 Network Guru Member

    -s ADDRESS
               Specifies the source address used for creating sockets.  In listen mode and tunnel mode this switch specifies the bound address, and it is generally a good idea not to specify this, which causes netcat to bind to a
               generic interface.  In the connect mode, this switch is used to specify the source address for connecting to the outside world.  Again, if it's not specified a proper address for the destination route will be used.
  5. Telex123

    Telex123 Networkin' Nut Member

    I had really hoped the URL technique was available!

    To the folks who suggested netcat, I'm going to need some more handholding because I'm finding it as complicated as I had suspected. I apologize in advance for some of the elementary Linux and shell scripting questions.
    1. BusyBox already has nc but lacks the features I need (listen on a port) so I'll need to install Rodney Hester's version somewhere on my router. Suggestions? In JFFS or a CIFS share? What are the implications when it comes time to upgrade Tomato? From what little I've read, I believe I have to ensure JFFS is empty and CIFS is unmounted before upgrading firmware?
    2. Let's assume I use a command string that looks something like this:
      nc -L -p 12345 -s | myscript
      If I've understood what I've read so far, it will listen on port 12345, only on the interface and pipe received data to myscript. I need the whole thing to run shortly after the router starts up but it needs to run in the background. Do I just append an ampersand (&) to the end of the command string?
    3. I believe myscript must also reside in JFFS or a CIFS share?
    4. Once myscript is executed, I assume the received data is held in the $1 parameter?
    I appreciate your help!

    I'll also need help with the command string. I tried what I posted above, on a Windows PC, and it does not pipe the received data to the script (batch) file. It runs the batch file immediately and then does nothing. Fail.
  6. Telex123

    Telex123 Networkin' Nut Member

  7. Telex123

    Telex123 Networkin' Nut Member

    It seems like every time I find an open door I discover it leads nowhere!

    The "How to expand webserver capabilities" thread looked very promising because I envisioned calling a URL that would execute a script on Tomato (actually just a single command "wl radio off"). Then I discovered this thread "Running a script from the browser" and it states that it is not possible to call a URL and have it run a script in Tomato.

    What a disappointment! Can someone confirm/refute this conclusion?

    It appears the only way to remotely trigger a command, or script, on Tomato is via telnet or ssh. Is that a fair assessment of Tomato's 'remote triggering' capabilities?
  8. lancethepants

    lancethepants Network Guru Member

    If you're not afraid of installing optware, I can see this as a possibility.

    You could install the web server of your choice ie. Apache/nginx/etc. Install and configure PHP to work with the your web server. Script what you want to do in PHP, either to take in some command from the URL, or hardset a URL to a certain function.

    I think you could have some success trying it this way.
  9. Telex123

    Telex123 Networkin' Nut Member

    Thanks for the suggestion. A more full-featured web-server would certainly open the door to other possibilities beyond my current one. However, I had hoped to avoid installing more apps on the router if no other reason than to simplify my life when it came time to upgrading its firmware.

    I decided to explore the SSH route. After looking high and low for free, scriptable SSH clients, I found two solutions:
    Bitvise Tunnelier

    Tunnelier includes a console program called "sexec". Assuming you have already manually logged in and saved the session details (hostname, username, password, etc) in a file, here is all that's needed to disable the router's wireless radio:
    sexec -profile="MyRouter.tpl" -cmd="wl radio off"

    My e3000 has two radios and "wl radio off" only turns off the 2.4GHz radio. "wl -i eth2 radio off" takes care of the 5GHz radio.

    The latest version of WinSCP includes .NET and COM libraries. Here's the code, in VBScript, to turn off the first radio. FWIW, 90% of it is to set up the connection and only the last line executes the desired command.
    For my Home Automation software, WinSCP's COM library is a better fit but Tunnelier's "sexec" does the job equally well.
  10. Planiwa

    Planiwa LI Guru Member

    Here is an example of how to control one Unix host (or GNU/Linux host) from another. I will show how to control a Mac from a Tomato router. I will leave out the sordid details, such as how to do it continually. Suppose the hostname of the Mac is Mac, and we'll use port 3899:

    On the Mac, start a listener, for example:

    nc -l 3899 |sh
    (instead of sh you want to use a less generous script that only does ceratin things, but sh will do to illustrate the principle.)

    On the Tomato router, issue a command, for example:

    echo say "Hello world, this is Tomato speaking." | nc Mac 3899
    The Mac's nc will receive this text from Tomato's nc and feed it to the shell, which will execute it, and thus the Mac will speak the words in quotes. That's all. The rest is inessential detail.

    Of course, not every computer runs Unix, but for the ones that do, it's as simple as that.
  11. Telex123

    Telex123 Networkin' Nut Member

    Did all that but a few more steps are needed to make it 'production worthy'.

    How would you use nc on Tomato so that it runs in the background? How do you pipe what it receives as a parameter to a script? Where's the best place to store the script on the router?
  12. Planiwa

    Planiwa LI Guru Member

    First of all -- I am not providing a "product", for a particular "application", but offering ideas and approaches that may inspire some others to explore further. I'm really talking about a process rather than a product. (The OP's problem is solved, with what appears to be the best solution for that particular setting).

    The example I gave above is complete, and will succeed. There is a huge difference between making something work in a "proof of concept" prototype setting and then working out the sordid details, and the other approach.

    (This may well be a difference in temperament. :)

    Tomato is wonderful in that it provides a (rudimentary) Unix interface (in addition to the very pleasant GUI).

    However, much of the Unix functionality was sacrificed (space constraints), which can make some easy tasks much less easy.

    There are so many different builds, but not one that provides a more functional Unix environment. Much of it is as simple as *not* removing pretty standard functionality in the Busybox config. I think the main reason may be that few users and few developers have a strong interest in that. Perhaps those who do are over in the OpenWRT camp? I regard the Tomato environment as similar to the Mac environment -- very nice GUI, with Unix functionality available to those users who want it. Except that Tomato's Unix environment is not fully functional.)

    So, that leaves more details to worry about with Tomato.

    For Tomato, I would like to suggest ncat, from the nmap project as a replacement, instead of the native nc.
    (Look for it here: http://multics.minidns.net/tomato/ [PRECOMPILED-static/nmap/] )

    Addressing questions:
    How does one run a Unix command in the background? Append & to the command line.
    How does one keep it running after one logs out? Prepend nohup.
    Parameter? Parameters are for invoking programs. Programs read standard input and write to standard output, etc. The nc (netcat, ncat) processes communicate with each other that way.
    Where to store it on the router? There are two implied questions here: Where to store it and how to invoke it. Once fully developed it might be invoked from a script, posibly restarted at firewall. So, the code could be there, which would cause it to be stored in NVRAM. If it is long, it could be in any convenient place, such as JFFS, USB device, or on a host on the network, from which it might be copied to the router's root home directory when needed.
  13. Telex123

    Telex123 Networkin' Nut Member

    Such is life, many ideas but few solutions.

    Thanks for your help.
  14. GodRox

    GodRox Networkin' Nut Member

    I am into home automation as well. I have often thought it would be great to be able to interface with my Tomato router/firmware from my HA interface. I have very little to bring to the table, other than, "if you figure it out, please share!" The possibilities are endless, as far as I'm concerned.


Share This Page