Skip to content

Instantly share code, notes, and snippets.

@borekon
Last active February 12, 2020 13:08
Show Gist options
  • Save borekon/1f7c5090311ea825460f7c02ccbd7ee9 to your computer and use it in GitHub Desktop.
Save borekon/1f7c5090311ea825460f7c02ccbd7ee9 to your computer and use it in GitHub Desktop.
PHP script to update dynamic IP on afraid's freedns service. Works on PHP 7.0
<?php
/*************************************************************************/
/* Copyright (c) 2020 by Alfonso Vivancos */
/* Based on the work by Giovanni Derks (http://derks.me.uk/) */
/* https://vivancos.eu */
/* */
/* This program is free software. You can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation; either version 2 of the License. */
/*************************************************************************/
$using_router=1;
$get_ip_from_url="http://ifconfig.co/ip";
$code[]="YOUR-STRING-HERE";
// --------------------------------------------------------------------------
echo("------------------------------------------------------------\n");
echo("FreeDNS update scritp\n");
echo("------------------------------------------------------------\n");
echo("Developed by Giovanni Derks\n");
echo("Modified by Alfonso Vivancos\n");
echo("Released under the terms of the GNU General Public License.\n");
echo("------------------------------------------------------------\n\n");
$done=0;
$rcnt=0;
while((!$done) && ($rcnt < 15)) {
echo("Finding your IP... ");
if (!$using_router) {
$ip=$_SERVER["REMOTE_ADDR"];
}
else {
$ip=@file_get_contents($get_ip_from_url);
}
if ($ip != "")
echo(" [ OK ]");
else
echo("[ Error ]");
echo("\n\nYour ip is: ".$ip."\n\n");
if (file_exists("last_ip.txt"))
$last_ip=trim(file_get_contents("last_ip.txt"));
else
$last_ip="";
if (($last_ip != $ip) && ($ip != "")) {
foreach($code as $key=>$val) {
exec("wget \"http://freedns.afraid.org/dynamic/update.php?".$val."\" -O update.php -q");
echo(file_get_contents("update.php"));
exec("rm update.php");
echo("\n\n");
}
save_file("last_ip.txt", $ip);
if (file_exists("log.txt"))
$log=file_get_contents("log.txt");
else
$log="";
$lines=explode("\n", $log);
$log=date("Y-m-d H:i:s")." - Updated: ".$ip."\n";
for($i=0; $i<count($lines); $i++) {
if($lines[$i] != "") $log.=$lines[$i]."\n";
}
save_file("log.txt", $log);
}
if ($ip == "") {
echo("Ip not found, will try again in 20 seconds... [ ".($rcnt+1)."/15 ]\n\n");
sleep(20);
}
else {
echo("\n------------------------------------------------------------\n");
echo("All done!");
echo("\n------------------------------------------------------------\n\n");
$done=1;
}
$rcnt++;
}
if (!$done) echo("Giving up...\n\n");
// -------------------------------------------------------------------------
function save_file($fn, $txt) {
if ($handle=fopen($fn, 'w+')) {
if (fwrite($handle, $txt) === FALSE) {
echo "\n - Error: cannot write to file [ $fn ]\n";
exit;
}
fclose($handle);
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment