#!/usr/local/bin/php -q
<?

/* 
 * Joshua Anderson <dnsadmin@afraid.org>
 * FreeDNS.afraid.org
 */


/*

    Usage Instructions : 

    1). Get your 'updateurl'
        - Go to http://freedns.afraid.org/
        - Click 'Dynamic DNS'
        - All 'A' records in your account should be in the list, copy
        the 'Direct URL' location on the host you want to use, and use
        that URL as your updateurl

    2). Set your 'getipcmd'
        - On the command line, use whatever means necessary to check
        your current IP address, and put that in the getipcmd variable

    3). Create cache last_ip file
        - In the directory you are running this script from, make sure the
        file lastip.txt exists and was created by the same user running the
        script

    4). Set check interval
        - The sleep(5) below is 5 seconds to check if the IP has changed,
        you can raise or lower this depending on the method you use to
        check your IP in getipcmd (step 2), since the getipcmd actually
        runs ifconfig on the local box, 5 seconds is OK as it does not
        use any external resources.

    5). Run
        - Recommend running program in a GNU 'screen' session so you can
        re-attach to it to check on it's status.  This program requires
        PHP to be installed.  You can run it like so : php -q lastip.php

*/

$updateurl 'http://freedns.afraid.org/dynamic/update.php?';
$getipcmd "ifconfig fxp0 | grep 'inet ' | awk '{print $2}'";

$last_ip file('lastip.txt');
$last_ip $last_ip[0];
$last_ip substr($last_ip0, -1);


while (
1) {
    
$ip shell_exec($getipcmd);
    
$ip substr($ip0, -1);

    if (
$last_ip != $ip) {
        
$date date('Y-m-d H:i:s');
        
$handle fopen('lastip.txt''w');
        
fwrite($handle$ip "\n");
        
fclose($handle);
        print 
"$date - IP has changed from $last_ip to $ip.\n";
        print 
"Grabbing url $updateurl...\n";
        
$remote file($updateurl);
        
$results implode($remote"");
        print 
"$results\n";
        
$last_ip $ip;
    } else {
        print 
"IP $ip has not changed.\n";
    }


    
sleep (5);
}

?>