#!/usr/bin/php
<?php
/* This is a modified version of get-ip2 by Andrew Hooper.
 0 2* Besides grabbing the WAN ip from a router page, I decided to use SNMP to
grab it instead. Thus, Work will be done faster.
 0 2* You'll need PHP-CGI 4.3.0 and above, compiled with snmp support and a
router that supports SNMP. This script can be added as a cron job and is
recommended to
 0 2* run it every minute, it won't eat up much resources, though.
 0 2* Execute this script like this:
 0 2*
 0 2* [shell]$ php /path/to/the/script/afraid.org.php
 0 2* modified by Ching Yonghan, release date: 15 March 2005
 0 2*/

$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
$router_ip '192.168.1.1'# IP of your router;
$oid 'ipAdEntAddr.219.95'# change 219.95 to the part that never changes
whenever you get a new IP

# Below is your update url, you can add it as many as you like :)
$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

$a snmpwalk($router_ip"public"$oid);
$ip substr($a[0], 11);
$logmsg date('Y-m-d H:i:s')." - New IP detected: ".$ip."\n";

$last_IP = @file_get_contents($stat_file);

if(
$debug=='ON'){echo "Last IP was : " $last_IP"\n\r";}


if(
$ip == $last_IP){
if(
$debug=='ON'){echo "No change to IP : " $ip ."\n\r";}
}else{
# Create or Alter the status file.
if($debug=='ON'){echo "Changing IP from : " $last_IP " to : " $ip "\
n"
;}
$fname $stat_file;
$fp fopen($fname,'w+');
$log_ip $ip;
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";
if(
$debug=='ON'){echo $dynamic_update "\n";}
}
fwrite($fp,$log_ip);
fclose($fp);

}

?>