I have a dynamic IP address, changes every time my router is restarted. I have complete access to my router. What I do every morning is manually execute this script from terminal to get the new updated leased IP. Lease time is about a week if we don't turn off the router. I have to check everyday for new IP. Our LAN's workstations need to use the leased external IP in order for their software to work right.
I have setup two separate php scripts
1) A plain file that contains the value of the actual ip.php
<?php
$ip="200.121.215.117";
?>
2) A middleware script that evaluates if leased external IP has changed:
<?php
include 'ip.php';
$oldip = $ip;
$newip = $_SERVER['REMOTE_ADDR'];
echo "Old IP : ".$oldip. " & Current IP : ". $newip;
if ($oldip <> $newip) {
echo "<br>Different IP found, updating workstations IPs done...<br>";
unlink('ip.php');
$file = "ip.php";
$a = fopen($file, "a");
fwrite($a,"<?php"."
");
fwrite($a,"$"."ip=\"".$newip."\";
");
fwrite($a,"?>"."
");
fclose($a);
$a = fopen($file, "r");
fclose($a);
echo "<br> Set IP to :\"".$newip."\"
";
} else ($oldip == $newip) {
echo "<br>Same IP, nothing done";
}
?>
3) And finally access the IP as a text from any LAN workstation
include '/home/myweb/public_html/ip.php';
echo "IP=".$ip;
I was thinking on executing this script directly on the router via cron setup unless someone has a better and more efficient way to do it.