Bind your server`s dynamic ip address to both Linux and Windows)

Recently I built up a server for developing purposes, and after I set it up I realized that my ISP provides me with only a dynamic ip address. So the solution was obvious…set my router to use DynDNS(they has a free service of course). Of course it didn`t work(obviously as I`m writing this). DynDNS changed their API and my router didn`t have firmware update to fix it….So I decided to write an application which syncs the current IP address with dyndns using their API. I wrote the application on C# using Mono to run it on the  linux server. The code is just a mockup I`m open for any suggestions or optimizations…

What it actually does is check your current IP at and compares the result with the last checked IP(if any). If the IP got changed the application connects with the DynDNS API and renews the IP address.  You can change the period on which the application checks for IP changes from System.Threading.Thread.Sleep(). It is in ms so for 1 minute you should set it to System.Threading.Thread.Sleep(60000).

In order to use it you should change exampleusername, examplepassword and to your actual username, password and hostname and compile the Application(You can use Compilr if you don`t have Visual Studio installed).

using System;
using System.IO;
using System.Net;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using System.Text;
using System.Threading;

namespace ConsoleApplication1
 class Test
 static void Main(string[] args)
 int repeat = 1; //Used for the infinite loop
 Console.WriteLine("DynDNS sync system");
 string lastIP = "";
 string myRequestURL = string.Format("");
 string strippedIP2 = "";
 HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(myRequestURL);
 string myResponse = new StreamReader(myRequest.GetResponse().GetResponseStream()).ReadToEnd();
 Console.WriteLine("Your current IP is: ");
 string RegexPattern = @"<html><head><title>Current IP Check</title></head><body>Current IP Address: .*?</body></html>";
 System.Text.RegularExpressions.MatchCollection matches = System.Text.RegularExpressions.Regex.Matches(myResponse, RegexPattern, System.Text.RegularExpressions.RegexOptions.IgnoreCase);
 foreach (System.Text.RegularExpressions.Match match in matches)
 string strippedIP1 = Regex.Replace(match.Value, "<html><head><title>Current IP Check</title></head><body>Current IP Address: ", "");
 strippedIP2 = Regex.Replace(strippedIP1, "</body></html>", "");
 int ipComparison = string.Compare(strippedIP2, lastIP, true);

if (ipComparison != 0)
 lastIP = strippedIP2;
 Console.WriteLine("The IP address has changed...The new address is: "+strippedIP2);
 string dnsUpdateURL =""+ strippedIP2 +"&wildcard=NOCHG&mx=NOCHG&backmx=NOCHG";
 // Get HTML data
 WebClient client = new WebClient();
 client.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
 client.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(Encoding.ASCII.GetBytes("exampleusername:examplepassword")));
 Stream data = client.OpenRead(dnsUpdateURL);
 StreamReader reader = new StreamReader(data);
 string str = "";
 str = reader.ReadLine();
 while (str != null)
 str = reader.ReadLine();
 catch (WebException exp)
 Console.WriteLine(exp.Message, "Exception");
 Console.WriteLine("IP hasn`t changed!");
 System.Threading.Thread.Sleep(300000); //The period on which the application will re-check the IP
 while (repeat > 0);

So If your router doesn`t support DynDNS and you want your server/PC to be accessible over the internet for free this is the easiest way you can do that.

When I wrote this i found the API documentation(which is great btw) to be very helpful.
DynDNS API Documentation 

If anyone needs to ask a question, finds this article helpful, or needs the compiled source feel free to post a comment 🙂


Tags: , , , , ,

One response to “Bind your server`s dynamic ip address to both Linux and Windows)”

  1. Richard says :

    I’d suggest you use it can update on both router and pc if the earlier doesn’t support it. it’s free too.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: