Tag Archives: Python

Automatic monitoring and restarting of internet router

Obligatory blog post graphic, to make this more "interesting" 😛 Meanwhile, check out the really nice Tonido Plug at http://www.tonidoplug.com/

My internet connection goes down periodically, and I used to have to power cycle the router in order to fix that.  When it started to become too frequent it posed a problem, since I’m too lazy to keep going to the room (my wife too) to restart it.  There’s also the option of restarting the router via the web admin interface, but it required me to login, click to the page for restarting, and click “restart”!  Very complicated indeed for lazy people.

Inspired by this hack (Hack a Day) where the guy automated the physical power cycling process, I decided to automate mine too.  Since I have a Tonido plug which is almost always on, and I’ve just learnt Python too, I decided to go the scripting method.  As they say: to a man with a hammer, everything looks like a nail 😉

A couple of lessons learnt

I was caught by surprise by when reproducing the login and restart sequence exactly didn’t work, and I went so far as to reproduce ALL the requests made by a “normal human”.  It turned out (after 2 hours and a shower break) that things worked just fine when I simply converted the minimally needed POST parameters to GET parameters.  Nice classic web application hacking trick learnt from my old job as a web application ethical hacker I’d say.

Also, the restart sequence for my router turned out to not only need the form “POST” to request a restart, but also a subsequent request for the “restarting now” status page, interesting…


Note that before you use this, some reverse engineering of the web application calls is needed, and some Python coding too.  You have been forewarned!  Also, I’m not responsible for this script causing you direct/indirect damage in any way, so don’t come crying when your lawnmower starts to act crazy because you installed this script.  The script is released under the GPL, and can be downloaded here.

How to install/use

  • Edit ‘router_host’: ‘’, in line 8
  • Reverse engineer the web admin login and restart sequence, see what you need.  I used tools like a transparent proxy (Burp Suite), notepad and some brain grease.
  • Hack the restart_router() (lines 43-73) function in the python script according to your needs (you’re on your own here…  Alternatively you could offer me a good amount of Coke/chips for me to help you with the reverse engineering/coding somehow 😉 )
  • Copy into the Tonido plug’s /root directory (assume running as root, for simplicity’s sake)
  • SSH into the Tonido plug as root
  • # chmod 400 /root/internet_connection_monitor.py
  • # crontab -e
  • Add in this line: (makes the script run in the background, 4 minutes after every tonido plug reboot to give the router time to start up)
    @reboot sleep 4m && /usr/bin/python /root/internet_connection_monitor.py &
  • Press Alt-X, then “y” to save the new crontab
  • Reboot the Tonido plug
  • Profit!

What are the risks to note

The script basically is a hardcoded piece of info revealing the password and sequence to your login/router’s workings! Make sure the script is chmod’ed properly, and isn’t accessible via Tonido’s interfaces.  For me I don’t have this problem, since I don’t allow connecting to my Tonido from outside anyway, and people will have to brute force ssh public keys to get in…

Have fun!

DNS tracking with Python

For those who have need to keep track of changes (IP address changes and “status”) of a list of DNS hosts/addresses, I wrote a Python script to help with this task.

Please help by providing comments/ideas for improvement/thanks/death-threats in the comments section below.  Ok, maybe not the death-threats. 😉

Download and License

The current version of this script is v1.03, released under the GPL license.  Click here to download it.

This software is licensed under the CC-GNU GPL version 2.0 or later.

What it can do/Features:

  • Keep track of changes (IP addresses, status: existent/gone) to a list of specified domains
  • Resolve many many many domains into a greppable format for you!
  • Multithreaded in Python
  • Does logging to a logfile, and to console at the same time
  • Configurable to a certain extent

How to get and use:

  • Install the PyDNS library found in the Python Package Index, hosted at SourceForge
  • Grab a copy from the download section above and extract the script out
  • Configure the script if you need to (see below)
  • Create a text file named dns-list.txt in the same folder as the dns_tracker.py script, list the DNS hosts that you want to track in the dns-list.txt file, one host per line
  • Run the script from command line: python dns_tracker.py.  The log file written to would be called dns-track.log
  • Profit!
  • You can stop the script by pressing Ctrl-C in the console ONCE.  It will attempt to kill off the waiting threads and exit gracefully.

What you can configure:

  • All the editable options are in the section marked ##Editable options.  Some of the options are…
  • Use system configured resolvers: set ‘use_server’: False,
  • Use a DNS resolver of your choice: set ‘use_server’: True, and also ‘server’: ‘<your resolver>’,
  • The logfile name can be changed too ‘logfile’: ‘dns-track.log’,


    • v1.03 (15 Aug 2010)
  • fixed logging hierarchy!  now we can control console and file log levels!
    • v1.02 (10 Aug 2010)
  • added SERVFAIL to recognise as possible status
  • changed DNS resolving fail behaviour: retry 1min later
  • changed monitoring start behaviour (faster by using threads)
  • orphaned threads will also stop themselves if the main thread’s killed
    • v1.01 (3 Aug 2010)
  • demarcated editable options section (“##Editable options”)
  • changed logging to append instead of overwriting existing log
  • remember that CNAMEs are also extracted for comparison of changes
  • added in minimum delay checks to account for CNAMEs’ TTL being 0
  • adjusted logging levels for logfile, console still outputs everything
    • v1.00 (2 Aug 2010)
  • initial release!


  • Perhaps migrating configurable options out to command line parameters
    or a separate config file?

DNS lookups using Python

Using the PyDNS library found in Python Package Index and hosted at SourceForge, some code snippets for usage:

import DNS
reqobj = DNS.Request(name="blog.rayfoo.info")
resp = reqobj.req()

for i in resp.answers: print i
{'name': 'blog.rayfoo.info', 'data': '',
'typename': 'A', 'classstr': 'IN', 'ttl': 1790, 'type': 1,
'class': 1, 'rdlength': 4}

PDG.py 1.0 - blog.rayfoo.info 1
;; options: recurs
;; got answer:
;; -HEADER- opcode 0, status NOERROR, id 60299
;; flags: qr rd ra; Ques: 1, Ans: 1, Auth: 0, Addit: 0
;;      blog.rayfoo.info, type = A, class = IN

blog.rayfoo.info        1790    A



;; Total query time: 367 msec
;; To SERVER: <sanitized>
;; WHEN: Sat Jul 31 11:06:24 2010

>>> resp.args
{'protocol': 'udp', 'name': 'blog.rayfoo.info',
'server': 'sanitized', 'rd': 1, 'opcode': 0,
'timeout': 30, 'timing': 1, 'elapsed': 367.52486228942871,
'qtype': 1, 'port': 53}