Not so long ago I decided to upgrade my router with a nice shiny new one. I opted for the Belkin N+ (the F5D8635uk4A to be overly specific). It’s a nice router, with a bundle of features that I don’t really intend to use. One feature that’s great tho’ is the built-in USB port, which allows the addition of either Flash Memory or a real USB HDD, which then acts as Network Attached Storage. Fabulous. I just wondered if it was possible to kick off a download directly to the flash drive, via the router, so that I could turn off all of my PCs and keep the download going…
The answer is yes – with a little playing…
Now, I’m happy to hack around with the devices I purchase, tho’ I draw the line at doing anything potentially damaging such as installing custom firmware – I’m simply a wuss. For example, I upgraded my router to the latest official firmware only to find that changing the default blank password was seemingly impossible. That’s something akin to a “major security balls-up”, so using my knowledge of hackery-pokery I managed to force a password change (which I’ll detail how to do in another post, unless Belkin release working firmware in the meantime).
One of the things I like to try when I get a new router, or any network device, is to telnet in and see what it’s made of. As with my previous routers the bulk of the system is made of Busybox, a neat set of common Unix tools designed to work on minimal and embedded systems – such as routers. Of course my router doesn’t contain the latest version of Busybox, I wouldn’t expect it to, but the selection of commands it provides is quite nice. The important command, which happily does exist, for my purposes is
wget is a program that allows you to get a file from web or ftp-sites. The full program has a whole bundle of features that allow site-mirroring, but the Busybox implementation is a lot simpler.
To get a file is easy – you literally telnet in, change to the directory of the USB drive (on my machine it’s
/ramdisk/tmp/dev/sda1), and run
wget http://site/file.zip. The problem here is that you have to remain telnet’d in until the download finishes. That’s where we use a little scripting know-how.
When you exit a telnet session, and of the child processes that you’ve started will be sent a “Hang-Up” signal, or SIGHUP. This tells them to finish what they’re doing and exit cleanly. On most Unix systems you can use the command
nohup to make the program ignore this signal and carry on after you quit. But the version of Busybox compiled into the Belkin N+ by default doesn’t have ‘nohup’ installed. Instead we can use a feature that it does have to detect the SIGHUP, and when it happens to do nothing… meaning the process won’t be asked to exit.
(trap '' 1; wget -q http://site/file.zip) &
That command entered at the
$ prompt does the following:
- Trap the Hang-Up signal (signal code 1).
- When the signal occurs run
- Execute the
wgetcommand with the
-qoption to ensure there’s no output to the console.
&ensures it runs in the background, meaning you can happily quit your telnet session.
The command will begin, and carry on in the background until it’s finished – even if you decide to log out of your telnet session.
One oddity I found is that there’s no
fg commands, which allow you to put a suspended task into the background (and keep it running), and bring a background task into the foreground (allowing you to Ctrl-C it to death). This means that to end a running
wget you need to get the process ID and
kill it. But that can be left as an exercise for the reader.
Now… should I write a simple application that will initiate a download for you, without the need to get into the nitty-gritty of telnet? Hmm.