{"id":1713,"date":"2019-04-02T07:08:46","date_gmt":"2019-04-02T06:08:46","guid":{"rendered":"https:\/\/mehm.net\/blog\/?p=1713"},"modified":"2019-04-02T07:08:46","modified_gmt":"2019-04-02T06:08:46","slug":"changing-fritzbox-static-routes-with-node-js","status":"publish","type":"post","link":"https:\/\/mehm.net\/blog\/?p=1713","title":{"rendered":"Changing Fritz!Box static routes with node.js"},"content":{"rendered":"\n<p>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:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" width=\"817\" height=\"350\" src=\"https:\/\/mehm.net\/blog\/wp-content\/uploads\/2019\/04\/image.png\" alt=\"\" class=\"wp-image-1729\" srcset=\"https:\/\/mehm.net\/blog\/wp-content\/uploads\/2019\/04\/image.png 817w, https:\/\/mehm.net\/blog\/wp-content\/uploads\/2019\/04\/image-300x129.png 300w, https:\/\/mehm.net\/blog\/wp-content\/uploads\/2019\/04\/image-768x329.png 768w, https:\/\/mehm.net\/blog\/wp-content\/uploads\/2019\/04\/image-720x308.png 720w\" sizes=\"(max-width: 817px) 100vw, 817px\" \/><\/figure>\n\n\n\n<p>I first tried the route via <a href=\"https:\/\/avm.de\/service\/schnittstellen\/\">TR-064<\/a> 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.<\/p>\n\n\n\n<p>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 <a href=\"https:\/\/github.com\/cheeriojs\/cheerio\">Cheerio<\/a> as a tool for web scraping, which allows to navigate through web sites in a similar way to jquery.<\/p>\n\n\n\n<p><strong>Signing in<\/strong><\/p>\n\n\n\n<p>The first step is to properly sign in. For this, the Fritz!Box implements a <a href=\"https:\/\/avm.de\/fileadmin\/user_upload\/Global\/Service\/Schnittstellen\/AVM_Technical_Note_-_Session_ID.pdf\">challenge-response system<\/a>. 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.<\/p>\n\n\n\n<p><strong>Getting pages<\/strong><\/p>\n\n\n\n<p>The Fritz!Box web interface builds a lot on AJAX. Pretty much all information is available by making request to <\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\/data.lua<\/pre>\n\n\n\n<p>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<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"> page: 'static_route_table' <\/pre>\n\n\n\n<p><strong>Uploading changes<\/strong><\/p>\n\n\n\n<p>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.<\/p>\n\n\n\n<p>You can find the result on my github account <a href=\"https:\/\/github.com\/SpookyFM\/fritz_route\">here<\/a> as well as on <a href=\"https:\/\/www.npmjs.com\/package\/fritz-route\">npm<\/a> .  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.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":1729,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/mehm.net\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1713"}],"collection":[{"href":"https:\/\/mehm.net\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mehm.net\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mehm.net\/blog\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/mehm.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1713"}],"version-history":[{"count":3,"href":"https:\/\/mehm.net\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1713\/revisions"}],"predecessor-version":[{"id":1730,"href":"https:\/\/mehm.net\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1713\/revisions\/1730"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/mehm.net\/blog\/index.php?rest_route=\/wp\/v2\/media\/1729"}],"wp:attachment":[{"href":"https:\/\/mehm.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1713"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mehm.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1713"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mehm.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1713"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}