Changing Fritz!Box static routes with node.js

For a personal project related to home automation, I wanted to be able to set up static routes on my Fritz!Box from the command line. So, basically to automate the following part of the Fritz!Box settings:

I first tried the route via TR-064 which is a protocol intended to be used for remotely configuring DSL devices, which should cover my use case. However, even after spending hours on different approaches, I was never able to reliably create and update routes. I even contacted the service of AVM but did not find anything out.

Instead, I chose to go another route, while reading about automating the Fritz!Box, I noticed that the web interface lends itself relatively well for remotely signing in. I used Cheerio as a tool for web scraping, which allows to navigate through web sites in a similar way to jquery.

Signing in

The first step is to properly sign in. For this, the Fritz!Box implements a challenge-response system. You receive a challenge which you have to answer with a response that builds a hash out of the challenge and your password. After this point, you get a Session ID (SID) that you need to supply with each call to the API afterwards.

Getting pages

The Fritz!Box web interface builds a lot on AJAX. Pretty much all information is available by making request to

/data.lua

on the fritzbox and the correct parameters. For example, you get the current static routes by sending a POST request to this page with the parameter

 page: 'static_route_table' 

Uploading changes

The same is true for making changes, you just call the same page as above with the correct parameters. The parameters are very much aligned with the way the UI works (i.e. 4 fields for the 4 parts of an IPv4 IP address), there is some conversion needed, but nothing that is not straightforward.

You can find the result on my github account here as well as on npm . I was only able to test the script on my FB 7490, so your mileage with other boxes and versions of the operating system might vary.