Track your Dynamic IP

dynamic-vs-static-ip

For several reasons I’d like to keep my dynamic IP instead of install a static IP, but I had to use a service such as easydns.com, no-ip.org etc. The only thing I was needed was just the current IP of my home router. So, I had this simple idea which when the linux box detects an IP change, then the script updates the web page. So, when I want to get informed about the ip of my home router, I’m visiting the address example.com/myip/index.php. For this, I made a php script which runs nicely.

Initially, we need a linux box (I’m using a VM) and a hosting plan somewhere over the Internet.

So, we have to install two scripts. The first one at the web site, and the other to my linux box.

Linux box php script:

<?php
$current_ip = file_get_contents('http://wtfismyip.com/text/');
$stored_ip = file_get_contents('http://example.com/myip/index.php');
$current_ip = substr($current_ip, 0, -1);
$pos = strpos($current_ip, $stored_ip);
if($pos !== 0)
{
 file_get_contents("http://example.com/myip/index.php?ip=$current_ip");
}

?>

web site script:

<?php
if(isset($_GET[ip]))
{
 $fp = fopen('data.txt', 'w');
 fwrite($fp, "$_GET[ip]");
 fclose($fp);
}
$filename = "data.txt";
$handle = fopen($filename, "r");
$contents = fread($handle, filesize($filename));
fclose($handle);
echo $contents;

 

Finally,  put the linux script in a crontab  job running every 5 mins and you’re done!

Now, if you’ll visit your web site example.com/myip you’ll get the ip of your home router.So simple!

enjoy it!