Router scripting

Another thing that's been bugging me for a while on the tech side of things at home is my router. It's a Zyxel 2602 and while the interface is shiny and the setup is fairly easy, the damn thing has a tendency to atrophy and die after some days of uptime ... if there's been quite a bit of traffic going through it. In the spirit of new energy I wanted to do something about it.

Now, although I'm clearly a geek and like my gadgets, I'm not loaded with money, so buying a new shiny toy is not an option. Apart from that, I have a hard time accepting that it shouldn't be possible to make the Zyxel do the job properly. Well, maybe not properly - I have no intension of messing with the firmware (apart from updates whenever one's available/needed).

No, what I had in mind was scripting the router to reboot regularly. As I've just moved my company site off the server at home there's no problem if the router goes down for about a minute a day, in the middle of the night. So the question became one of: how to script it?

Possible Solution 1

I've been using Selenium for various projects, testing frontends and general functionality. Selenium allows you to script actions and it's basically ideal for a task like this: log into the webclient, find the right tab, click the right button, done.

Only problem is that installing this to run headless on my server every night means installing SeleniumRC and various other things ... in short, much too much work for just scripting a router reboot. If I already had Selenium installed there, then no problem - but unfortunately I don't.

Possible Solution 2

Instead of installing Selenium, one could also give wget a try - installing is a simple

sudo apt-get install wget

if the program isn't already installed, that is. After that, toying with various command line options got me logged into the routers webclient and requesting the reboot ... but no dice. For anyone interested, the options you'll need are

  • --save-cookies file / --load-cookies file
  • --keep-session-cookies
  • --post-data='blah=blah&moreblah=moreblah'

It didn't particularly look as if Zyxel actually cares about the cookies, but you never know if they'll come in handy later.

Possible Solution 3

At this point I started looking for alternative ideas - and that's when I remembered that Zyxel routers provide quite a few different protocols for access (like most other routers). So I checked out the telnet interface and quickly found myself wondering exactly what was going on ... the only help provided is lists of commands. No description, no anything. In my battle to find out how to reset things, this led me among other things to a factory reset of settings ... fairly annoying (don't issue sys default unless you've got settings backed up!). However, googling for info on Zyxel routers finally led me to a pdf with some goodies, although for a different model. It was worth a shot though, as the command looked as if it might work - and indeed, the Zyxel 2602 will happily respond to a sys reboot issued over telnet. That meant the task was down to scripting telnet - which turned out quite easy. Here's the oneliner I ended up with

(cat /path/to/passwordfile; sleep 1; echo sys reboot; sleep 1) | telnet 192.168.1.1

The first bit echoes the details needed for the router - after the pipe there's the actual connection. The sleep commands are there to allow the router to echo back a response before reacting on it - without them you wouldn't do anything. To avoid having the password stored in the shell history I use a file for storing it in ... not that much more secure, but at least you don't get surprised by where it moves to.

Done

A little bit of researching, some vim'ing and there: a script to restart my router every night at 5. Only thing left is scheduling the cron job :)

social