<?PHP
#version 0.02b    07/04/2003
# get-ip2 was originally written for a Dynalink RTA300W but the router died, It was then replaced
# and the wireless part of the router was faulty so I gave up and reconfigured it for use with an
# Alcatel Speedtouch Home that was upgraded to a Pro.
# It should work on most ADSL Routers providing there is a page that lists the remote IP on its own.
# Further modification may be made to this script however if you make any modifications please send 
# me a copy ( send any updates or changes to updateip@borg.co.nz )
# the script can be setup as a cron job and called via wget or lynx. Its up to you how
# often you want it to refresh but I'm intending to have it refresh every minute.

# This scipt is copywrite (C) of Andrew Hooper 2003 updateip@borg.co.nz.

# Use it at your own rist, I take no responsibility problems the script my introduce or security holes.

$stat_file 'remote_ip';        # Name of the status file (apache must have write permissions in the directory)
$log_file 'dynamic_ip.log';    # log the history to this file

$DSL_Username '';        # ADSL Router Username
$DSL_Password '';        # ADSL Router Password
$DSL_Address 'http://192.168.0.1/cgi/router/';    #U RL on the router that displays the remote IP

$IP_Update_URL[] = 'http://freedns.afraid.org/dynamic/update.php?QUERY_STRING';    # URL to open for updating the IP
$IP_Update_URL[] = 'http://freedns.afraid.org/dynamic/update.php?QUERY_STRING';    # URL to open for updating the IP
$IP_Update_URL[] = 'http://freedns.afraid.org/dynamic/update.php?QUERY_STRING';    # URL to open for updating the IP

$debug 'ON';            #Debuging ON / OFF

##################################################################
##       You should not need to edit beyond this point          ##
##################################################################

if(ereg('(http:\/\/)(.*)',$DSL_Address,$DSL_regs)){

    if(!
$DSL_Username && !$DSL_Password){
        
$DSL_url $DSL_regs[1] . $DSL_regs[2];        # If there is no Username and Password
    
}else{
        
$DSL_url $DSL_regs[1] . $DSL_Username ":" $DSL_Password "@" $DSL_regs[2];        # if there is a Username and Password
    
}
    
    
$DSL_Status implode('',file($DSL_url));
    
$DSL_Status strip_tags($DSL_Status'');

    
$ipok 0;

    if(
ereg('([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})',$DSL_Status,$IP_regs)){
      for(
$i=1$i 5$i++){
        
$ip_segment[$i] = intval($IP_regs[$i]);

        if(
$ip_segment[$i] <= 255$ipok++;
      }
      
$ip implode("."$ip_segment);
    }
}

$lastIP_Status implode('',file($stat_file));

if(
ereg('([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})',$lastIP_Status,$lastIP_regs)){

    if(
$debug='ON'){echo "Last IP was : " $lastIP_regs[0] . "<br>\n\r";}
    
    
$last_IP $lastIP_regs[0];
}

if(
$ip == $last_IP){
    if(
$debug='ON'){echo "No change to IP : " $ip ."<br>\n\r";}
}else{
# Create or Alter the status file.
    
if($debug='ON'){echo "Changing IP from : " $last_IP " to : " $ip "<br>\n\r";}
    
$fname $stat_file;
    
$fp fopen($fname,'w+');
    
$log_ip $ip " Date: " date("F j, Y, g:i a") . "\n\r";
    
fwrite($fp,$log_ip);
    
fclose($fp);

    
$fname $log_file;
    
$fp fopen($fname,'a');

    while(list(
$key$val) = each($IP_Update_URL)){
      
$dynamic_update implode('',file($val));
# Create and or write to the log file.
      
$log_ip "Date: " date("F j, Y, g:i a") . " - Response :" $dynamic_update "\n\r";
      if(
$debug='ON'){echo $dynamic_update "<br>\n\r";}
    }
    
fwrite($fp,$log_ip);
    
fclose($fp);

}


?>