Dynamic IP nameserver updater, with a tweet

I have a server connected to the Internet over a dynamic IP ADSL connection. Now and then the IP address changes and name servers need to be updated so I can access the server from outside. This updater periodically checks the IP address, informs a dynamic IP name service when changes occur, and tweets me about it.
The server can easily find the present address by accessing a URL at http://www.whatismyip.com. The response is nothing but a clean ascii string representing the IP address. This is compared with the previously saved address. If they match there is nothing more to do. However, if they differ the name service must be updated.

The dynamic name service I use is http://www.afraid.org. Their basic service is free. The name sever is updated by accessing their site with a URL specific to my server. The key included with the URL combined with the source IP address cause the name service to update the record. Eventually this change will trickle across the name server system of the Internet. This is a name server service and you only want to send an update after a change.

On the other hand, whatsmyip.com allows you to access them as frequently as every ten minutes.

I have written a Python program (dynip.py) that does this checking and updating for me. It also sends me a Twitter message to inform me of address changes. The message includes the new address which allows me to access the server using the IP address until the name servers have been updated. Tweets are sent by the  command line tool TTYtter.

Running dynip.py with any argument force name server updating even if the address hasn’t changed. This can be done a few times a month to tell the name service that the server is alive.

To adapt dynip.py for your site you must edit the updateUrl hash string according to the update script you can get from your page at afraid.org. You must also install TTYtter and run it once from the command line to obtain the .ttytterkey file from Twitter, read the doc. Move .ttytterkey to /root.

dynip.py may be installed in /usr/local/bin and run by cron. The following has been tested under Debian. To run dynip.py four times an hour and twice a month add these lines to /etc/crontab:

1,16,31,46 * * * * root /usr/local/bin/dynip.py
3 6 9,24 * * root /usr/local/bin/dynip.py force

The first line runs the program at the 1st, 16th, 31th and 46th minute of the hour. The second line at 06:03 the 9th and 24th day of the month.

These crontab entries run dynip.py as root. You can specify another user if the permissions of the files dynip.py accesses are properly set. Move the .ttytterkey to this user’s home directory.

0 Responses to “Dynamic IP nameserver updater, with a tweet”

  • No Comments

Leave a Reply