External IP Reporting from CENTOS

Much like my PHP example of external IP reporting, the following script performs exactly the same task, but in Centos. This combined with creating a custom system service should provide dynamic IP resolution even if the PC reboots.


#!/bin/bash
i=1
while [ $i == 1 ]
do
datestring=$(date –utc +%FT%TZ)
ipfile=”myip.txt”
ipdir=”/var/manage/”
logfile=/var/manage/myiplog.txt
oldip=$(cat $ipdir$ipfile)
echo “Getting IP… ”
wanip=$(dig +short EXTERNAL IP RESOLVER ADDRESS)
#if [ 1 -ne 1 ]
if [ $oldip != $wanip ]
then
echo My IP has changed! Saving change to file…
echo $wanip > $ipdir$ipfile
echo Connecting to FTP server…
lftp -u FTP_USERNAME,FTP_PASSWORD FTP_SITE -e ” lcd $ipdir; put $i$
echo Writing log…
echo “$datestring IP changed to $wanip” >> $logfile
echo Log written!
else
echo My IP is the same!
fi
echo Sleeping for 5 mins…
sleep 300s
done

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);
}
?>