Last active
December 4, 2024 22:24
-
-
Save corny/7a07f5ac901844bd20c9 to your computer and use it in GitHub Desktop.
Update script for dynv6.com to set your IPv4 address and IPv6 prefix
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh -e | |
hostname=$1 | |
device=$2 | |
file=$HOME/.dynv6.addr6 | |
[ -e $file ] && old=`cat $file` | |
if [ -z "$hostname" -o -z "$token" ]; then | |
echo "Usage: token=<your-authentication-token> [netmask=64] $0 your-name.dynv6.net [device]" | |
exit 1 | |
fi | |
if [ -z "$netmask" ]; then | |
netmask=128 | |
fi | |
if [ -n "$device" ]; then | |
device="dev $device" | |
fi | |
address=$(ip -6 addr list scope global $device | grep -v " fd" | sed -n 's/.*inet6 \([0-9a-f:]\+\).*/\1/p' | head -n 1) | |
if [ -e /usr/bin/curl ]; then | |
bin="curl -fsS" | |
elif [ -e /usr/bin/wget ]; then | |
bin="wget -O-" | |
else | |
echo "neither curl nor wget found" | |
exit 1 | |
fi | |
if [ -z "$address" ]; then | |
echo "no IPv6 address found" | |
exit 1 | |
fi | |
# address with netmask | |
current=$address/$netmask | |
if [ "$old" = "$current" ]; then | |
echo "IPv6 address unchanged" | |
exit | |
fi | |
# send addresses to dynv6 | |
$bin "http://dynv6.com/api/update?hostname=$hostname&ipv6=$current&token=$token" | |
$bin "http://ipv4.dynv6.com/api/update?hostname=$hostname&ipv4=auto&token=$token" | |
# save current address | |
echo $current > $file |
Here is a node.js implementation of the script, that can be built into an executable with pkg and scheduled using Systemd/Windows Task Scheduler
const os = require('os');
const ip6addr = (os.networkInterfaces())['Ethernet'][1]['address'];
const domain = '<domain>';
const username = '<token>';
const url = `https://dynv6.com/api/update?hostname=${domain}&token=${username}&ipv6=${ip6addr}`;
fetch(url, {
method: 'GET'
}).then((response) => {
response.text().then((data) => {
console.log(data);
});
}).catch((error) => {
console.error(error);
});
Like @slyfunky ... I Found my self with a bunch of linux-like scripts...
If you are trying to host with windows, take a look to this repo: https://github.com/slyfunky/Pango/blob/main/DNS.bat
I liked a lot your ideas, like getting dynamically the public ipv4.
Heres is my powershell code:
#!/usr/bin/env pwsh
# Currenty Only support for ipv4.
# zone / hostname
$hostname = $env:SERVICE_HOSTNAME
# HTTP Token got form https://dynv6.com/keys
$httpToken = $env:DYNV6_HTTP_TOKEN
# some service that returns your public ipv4
$publicIpProvider = $env:PUBLIC_IPv4_PROVIDER_SERVICE
if (-not $hostname) {
Write-Error "The environment variable 'SERVICE_HOSTNAME' is empty. Please set it before running the script."
exit 1
}
if (-not $httpToken) {
Write-Error "The environment variable 'DYNV6_HTTP_TOKEN' is empty. Please set it before running the script."
exit 1
}
if (-not $publicIpProvider) {
Write-Error "The environment variable 'PUBLIC_IPv4_PROVIDER_SERVICE' is empty. Please set it before running the script."
exit 1
}
# call to public ip provider
$publicIpProviderResponse = Invoke-WebRequest -Uri $publicIpProvider -ErrorAction Stop
# check the response status code
if ($publicIpProviderResponse.StatusCode -ne 200) {
Write-Error "Request failed with status code: $($response.StatusCode)"
Write-Error "Try with other value for PUBLIC_IPv4_PROVIDER_SERVICE"
exit 1
}
# get ipv4
$ipv4 = $publicIpProviderResponse.Content
# dynv6 http api ipv4 url
$dynv6HTTPApiIpv4 = "https://ipv4.dynv6.com/api/update?ipv4=$ipv4&zone=$hostname&token=$httpToken"
$dynv6Response = Invoke-WebRequest -Uri $dynv6HTTPApiIpv4 -ErrorAction Stop
$dynv6Response.StatusCode
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is my script (bash), free to use:
#!/bin/bash
bin="curl -fsS"
fqdn=<your_domain>
token='<Your_token>'
ipv4=
dig TXT +short o-o.myaddr.l.google.com @ns1.google.com | cut -f2 -d'"'
ipv6=
dig -6 TXT +short o-o.myaddr.l.google.com @ns1.google.com | cut -f2 -d'"'
echo "My Ipv6: "$ipv6
echo ""
if [[$ipv6 =~ ^([0-9a-fA-F]{0,4}:){1,7}[0-9a-fA-F]{0,4}$ ]]
then
echo "Updating $fqdn (ipv6)..."
$bin "http://dynv6.com/api/update?hostname=$fqdn&ipv6=$ipv6&token=$token"
echo ""
else
echo "Invalid IPv6 address"
fi
echo "My IPv4: "$ipv4
echo ""
if [[$ipv4 =~ ^[0-9]+.[0-9]+.[0-9]+.[0-9]+$ ]]
then
echo "Updating $fqdn (ipv4)..."
$bin "http://dynv6.com/api/update?hostname=$fqdn&token=$token&ipv4=$ipv4"
else
echo "Invalid IPv4 address"
fi
echo "End of DNS records update!"
exit 0
`
`