Matthew J. Kellett

Website Architect and Developer

Updating DynDNS using Inadyn

As you may know, having a dynamic IP address can be a real pain when it constantly changes. Fortunately there are several tools available to help manage these changes such as DynDNS or OpenDNS.

The problem I have solved with this script is that when using the free DynDNS service, using Inadyn, there is a limit on how often updates can be made to the service. In the event you make too many queries using cron for example, then the update service is blocked, meaning no futher updates.

This script makes use of an online IP tool to compare against before calling the service to update your IP address and DynDNS service.

The Code

#!/usr/bin/perl -w
use strict;

# this is the file that will contain the IP address
my $ip_file = "{PATH_TO_IP_FILE}/ip_address";

# this is the log file that allows you to see when updates have been made
my $ip_log = "{PATH_TO_LOG_FILE}/ip_log";

# fetch the current external IP address
my $external_ip = `wget -O - -q`;

# fetch the IP address previously set
my $old_ip = get_old_ip();

if ($external_ip ne $old_ip) {
        # ip's are different so update ipfile and call inadyn
        write_log_file("IP address was updated from $old_ip to $external_ip at " . localtime());
        exec("inadyn") || die("Could not excute the inadyn command: ".$!."\n");

sub write_new_ip {
        my $new_ip = shift();
        open IPFILE, ">$ip_file" || die("Could not open ip_file for writing: ".$!."\n");
        print IPFILE $new_ip || die("Could not write to the ip_file: ".$!."\n");
        close IPFILE;

sub get_old_ip {
        open IPFILE, "$ip_file" || die("Could not open ip_file for reading: ".$!."\n");
        my $old_ip = 0;
        while(<IPFILE>) {
                $old_ip = $_;
        close IPFILE;
        return $old_ip

sub write_log_file {
        my $message = shift();
        open IPLOG, ">>$ip_log" || die("Could not open log file for writing: ".$!."\n");
        print IPLOG "$message\n" || die("Could not write to ip_log file: ".$!."\n");
        close IPLOG;

The Explanation

The script bascially pulls the current external IP address of the server and compares that against the IP address previously recorded. If they are different then it will execute the inadyn service, update the IP record file and log the update in the file specified. If they match then no updates are made.

Please feel free to let me know you use this code so I can tell if this is actually useful

Facebook Comments

Like, follow, share us

Code Categories

Use the links below to view each of the different code snippet categories

Latest Code Snippets

Below are the latest code snippets that I have written


If you like this code snippet or use it then please donate something to me so I can continue updating this as well as creating new ones.