Automatic External IP Reporting from Windows

Its become very annoying, since most dynamic hostname services have changed to paid-for services, to keep loosing connectivity with satellite sites because their IP has changed.

As a work-around, I’ve set my windows web server (which already runs PHP) to report back to my paid hosting provider, what its current IP is. This linked with cron jobs with the hosting provider, should be able to update my satellite site IPs in DNS without any human intervention.

The following PHP resolves the IP from a text to browser IP recognition site (similar to googling “what is my IP”, but here is returned as a plain text file), checks for changes since the last time the script was run, then uploads the new IP to the root DNS site. It checks every 5 minutes, but this delay can be adjusted, and also reports what it has been doing to a local log file.

<?PHP
$i = 1;
while($i == 1){
$datestring = date('c');
$ipfile = "c:\manage\myip.txt";
$logfile = "c:\manage\myiplog.txt";
$remotefile = "myip.txt";
$oldip = file_get_contents($ipfile);
echo "Getting IP... \n";
$externalContent = file_get_contents('INSERT IP RESOLVER HERE');
preg_match('/Current IP Address: \[?([:.0-9a-fA-F]+)\]?/', $externalContent, $m);
if($oldip != $m[1]){
echo "My IP has changed! Saving Change to file... \n";
file_put_contents($ipfile, $m[1]);
echo "Connecting to FTP server... \n";
$conn_id = ftp_connect("MY FTP SERVER");
echo "Authenticating... \n";
$login_result = ftp_login($conn_id, "FTP USERNAME", "FTP PASSWORD");
echo "Uploading... \n";
if (ftp_put($conn_id, $remotefile, $ipfile, FTP_ASCII)) {
echo "Successfully Uploaded File... \n";
$ftpresult = " upload OK";
}
ftp_close($conn_id);
echo "Writing Log... ";
$longstring = $datestring." IP changed to ".$m[1].$ftpresult;
file_put_contents($logfile, $longstring, FILE_APPEND | LOCK_EX);
} else {
echo"My IP is the same! \nSleeping for 5 mins... \n";
}
sleep(300);
}
?>

Leave a Reply

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