Skip to content

Instantly share code, notes, and snippets.

@samuelthomas2774
Created March 1, 2017 17:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samuelthomas2774/517c42d5ab9c2f7072d0f7319269fddf to your computer and use it in GitHub Desktop.
Save samuelthomas2774/517c42d5ab9c2f7072d0f7319269fddf to your computer and use it in GitHub Desktop.
DNS Update Client

Setup

  1. Download this to your device This assumes it will be stored in /home/pi/duckdns.
  2. Change the $domains, $token, $ipv4_interface and $ipv6_interface variables in run.php
  3. Open root's crontab
    sudo crontab -e
    
    
  4. Add this line to the end
    */5 * * * * /home/pi/duckdns/run.sh >/dev/null 2>&1
    
    
    This will make the script run every five minutes - you can change this but you probably won't need to. You may need to change the path to run.sh.
<?php
$domains = "pi-1-thewalnutspi";
$token = "-- duckdns token --";
$ipv4_interface = "eth0";
$ipv6_interface = "teredo";
date_default_timezone_set("Europe/London");
echo date("Y-m-d H:i:s") . " Running update... user: " . get_current_user() . "\n";
// Get the IPv4 address
// You may want to use a site to get your public IPv4 address, instead of the one assigned to your device
//$input = system("ip addr show dev teredo | sed -e's/^.*inet6 \([^ ]*\)\/.*$/\1/;t;d'");
$input = shell_exec("ip addr show dev {$ipv4_interface}");
if(!preg_match_all("/^.*inet ([^ ]*)\/.*$/m", $input, $ip_addresses))
exit("Failed to read ip addresses: " . str_replace("\n", "\\n", $input) . "\n");
if(!isset($ip_addresses[1]) || !isset($ip_addresses[1][0]))
exit("Failed to find ip addresses: " . str_replace("\n", "\\n", $input) . "\n");
$ipv4 = $ip_address = $ip_addresses[1][0];
echo date("Y-m-d H:i:s") . " IP address: " . $ip_address . "\n";
// Get the IPv6 address
//$input = system("ip addr show dev teredo | sed -e's/^.*inet6 \([^ ]*\)\/.*$/\1/;t;d'");
$input = shell_exec("ip addr show dev {$ipv6_interface}");
if(!preg_match_all("/^.*inet6 ([^ ]*)\/.*$/m", $input, $ip_addresses))
exit("Failed to read ip addresses: " . str_replace("\n", "\\n", $input) . "\n");
if(!isset($ip_addresses[1]) || !isset($ip_addresses[1][0]))
exit("Failed to find ip addresses: " . str_replace("\n", "\\n", $input) . "\n");
$ipv6 = $ip_address = $ip_addresses[1][0];
echo date("Y-m-d H:i:s") . " IPv6 address: " . $ipv6 . "\n";
// Tell duckdns.org the new IPv4 / IPv6 addresses
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "https://www.duckdns.org/update?domains={$domains}&token={$token}&ip={$ipv4}&ipv6={$ipv6}&verbose=true");
curl_setopt($curl, CURLOPT_TIMEOUT, 10);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($curl);
echo date("Y-m-d H:i:s") . " Updated {$domains}.duckdns.org: status {$response}\n";
echo "\n";
php /home/pi/duckdns/run.php > /home/pi/duckdns/output.log
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment