Menu Close

How to check if your DDNS is updated with your ip

Using dynamic DNS for your site (http://dyn.com/dns/) you have to  install a simple utility that keep updated your domain with your current server ip (http://dyn.com/support/clients/).

But, what happen if for some reason the IP is not updated to your DDNS? Simple, all your site and services go down.

I wrote a simple script that avoid this, comparing current server IP with DDNS IP.

First we need another site that give us our current IP, there are lots of free services that do it.

I’ve another VPS in germany, so i put this simple script (ip.php) on it:

<?php
echo $_SERVER['REMOTE_ADDR'];
die;
?>

So now from our server we have to simple compare our IP (ANOTHERSERVER/ip.php) with the DDNS IP:

<?php
function getAddrByHost($host, $timeout = 3) {
 $query = `nslookup -timeout=$timeout -retry=1 $host`;
 if(preg_match('/\nAddress: (.*)\n/', $query, $matches))
 return trim($matches[1]);
 return $host;
}
$WANIP = file_get_contents("http://ANOTHERSERVER/ip.php");
$DNSIP = getAddrByHost("www.smartdomotik.com");
if ($WANIP == $DNSIP)
{
 echo 1;
}
else
{
 echo 0;
}
?>

The script returns zero if the compare failed.

You can put inside the condition all what you want (for example send a mail).

In my case this script is inside my alarm system in Smart Domotik and it send me a SMS Message.

Posted in Domotica e Cloud, Linux, News, Web Development

Leave a Reply

Your e-mail address will not be published. Required fields are marked *