Skip to content

Instantly share code, notes, and snippets.

@Manawyrm
Created April 5, 2024 20:42
Show Gist options
  • Save Manawyrm/d39966a6d470b29c2b277c9529f810eb to your computer and use it in GitHub Desktop.
Save Manawyrm/d39966a6d470b29c2b277c9529f810eb to your computer and use it in GitHub Desktop.
Update do.de FlexDNS from OpenWRT, with IPv4 and IPv6 prefix support (make sure /tmp is a tmpfs to not kill your flash!)
#!/usr/bin/env php-cli
<?php
$url = 'https://ddns.resellerinterface.de/';
$username = 'DDNS-';
$password = '';
$saved = json_decode(file_get_contents("/tmp/ddns.json"), true);
$ipv6json = `ip -6 --json addr show dev br-lan`;
$ipv6 = parse_json($ipv6json);
$ipv4json = `ip -4 --json addr show dev pppoe-wan`;
$ipv4 = parse_json($ipv4json);
$updateSuccess = true;
if ($ipv4 != $saved["ipv4"])
{
error_log("Updating ipv4 to " . $ipv4);
$return = file_get_contents($url . '?' . http_build_query([
"username" => $username,
"password" => $password,
"myip" => $ipv4
]));
error_log($return);
if (strpos($return, "good") === false && strpos($return, "nochg") === false)
{
$updateSuccess == false;
}
}
if ($ipv6 != $saved["ipv6"])
{
error_log("Updating ipv6 to " . $ipv6);
$return = file_get_contents($url . '?' . http_build_query([
"username" => $username,
"password" => $password,
"ip6lanprefix" => str_replace("::1", "::/64", $ipv6)
]));
error_log($return);
if (strpos($return, "good") === false && strpos($return, "nochg") === false)
{
$updateSuccess == false;
}
}
if ($updateSuccess)
{
file_put_contents("/tmp/ddns.json", json_encode([
"ipv6" => $ipv6,
"ipv4" => $ipv4
], JSON_PRETTY_PRINT));
}
function parse_json(string $json)
{
$data = json_decode($json, true);
foreach ($data[0]["addr_info"] as $ipaddress)
{
if ($ipaddress["scope"] == "global")
{
return $ipaddress["local"];
}
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment