updates to dynamic dns scripts, now with overrides, and even more similar.

This commit is contained in:
2023-09-21 10:18:02 -04:00
parent bbdc84261c
commit 3863f0772e
2 changed files with 57 additions and 32 deletions
+29 -10
View File
@@ -53,17 +53,21 @@
# IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
###################
# we need zone and token, we also accept, netmask, and overrides
param(
[string] $zone = $(throw "-zone is required"),
[string] $token = $(throw "-token is required"),
[string] $netmask = "128",
[string] $logfile = "$env:USERPROFILE\\.dynv6.log"
[string] $logfile = "$env:USERPROFILE\\.dynv6.log",
[string] $ipv4 = $null,
[string] $ipv6 = $null
)
# we store ip addresses in files, so we only update when there is a change
$file4 = "$env:USERPROFILE\\.dynv6.addr4"
$file6 = "$env:USERPROFILE\\.dynv6.addr6"
# load existing ip addresses, when available
if (Test-Path -Path $file4) {
$old4 = Get-Content -Path $file4
} else {
@@ -83,32 +87,47 @@ $update6 = [System.Uri]'https://dynv6.com/api/update'
Get-Date | Tee-Object -FilePath $logfile -Append | Out-Host
$current4 = Invoke-RestMethod -Method Get -Uri $lookup4
$current6 = Invoke-RestMethod -Method Get -Uri $lookup6
# lookup IPv4, will be skipped if variable already set
if ($ipv4 -eq $null) {
$ipv4 = Invoke-RestMethod -Method Get -Uri $lookup4
}
if ($old4 -eq $current4) {
# lookup IPv6, will be skipped if variable already set
if ($ipv6 -eq $null) {
$ipv6 = Invoke-RestMethod -Method Get -Uri $lookup6
}
# update IPv4, if changed
if ($old4 -eq $ipv4) {
Tee-Object -FilePath $logfile -Append -InputObject "IPv4 address unchanged" | Out-Host
} else {
# send IPv4 address to dynv6
Tee-Object -FilePath $logfile -Append -InputObject "Updating IPv4" | Out-Host
$body = @{
zone = $zone
token = $token
ipv4 = $current4
ipv4 = $ipv4
}
Invoke-RestMethod -Method Get -Uri $update4 -Body $body | Tee-Object -FilePath $logfile -Append | Out-Host
Out-File -FilePath $file4 -InputObject $current4
# save current IPv4 address
Out-File -FilePath $file4 -InputObject $ipv4
}
if ($old6 -eq $current6) {
# update IPv6, if changed
if ($old6 -eq $ipv6) {
Tee-Object -FilePath $logfile -Append -InputObject "IPv6 address unchanged" | Out-Host
} else {
# send IPv6 address to dynv6
Tee-Object -FilePath $logfile -Append -InputObject "Updating IPv6" | Out-Host
$body = @{
zone = $zone
token = $token
ipv6 = "$current6/$netmask"
ipv6 = "$ipv6/$netmask"
}
Invoke-RestMethod -Method Get -Uri $update6 -Body $body | Tee-Object -FilePath $logfile -Append | Out-Host
Out-File -FilePath $file6 -InputObject $current6
# save current IPv6 address
Out-File -FilePath $file6 -InputObject $ipv6
}
+28 -22
View File
@@ -1,21 +1,24 @@
#!/bin/sh
# we need zone and token, we also accept, netmask, and overrides
zone=$1
device=$2
scope=$3
# stop if we do not have the required information
if [ -z "$zone" -o -z "$token" ]; then
echo "Usage: token=<your-authentication-token> [netmask=64] [ipv4=<override>] [ipv6=<override>] $0 your-name.dynv6.net [device] [scope]"
exit 1
fi
# we store ip addresses in files, so we only update when there is a change
file4="$HOME/.dynv6.addr4"
file6="$HOME/.dynv6.addr6"
# load existing ip addresses, when available
[ -e $file4 ] && old4=`cat $file4`
[ -e $file6 ] && old6=`cat $file6`
# ensure we have required information
if [ -z "$zone" -o -z "$token" ]; then
echo "Usage: token=<your-authentication-token> [netmask=64] $0 your-name.dynv6.net [device] [scope]"
exit 1
fi
# [device] would be something like "eth0" or "wlan0"
if [ -n "$device" ]; then
device="dev $device"
@@ -45,36 +48,39 @@ else
lookup4="$bin https://ipinfo.io/ip"
fi
# lookup IPv4
ipv4=`$lookup4`
# lookup IPv4, will be skipped if variable already set
if [ -z "$ipv4" ]; then
echo "failed to lookup IPv4 address, continuing with 'auto'"
ipv4="auto"
fi
# calculate IPv6
address6=$(ip -6 addr list $scope $device | grep -v " fd" | sed -n 's/.*inet6 \([0-9a-f:]\+\).*/\1/p' | head -n 1)
if [ -z "$address6" ]; then
echo "no IPv6 address found"
exit 1
ipv4=`$lookup4`
if [ -z "$ipv4" ]; then
echo "failed to lookup IPv4 address, continuing with 'auto'"
ipv4="auto"
fi
fi
if [ -z "$netmask" ]; then
netmask="128"
fi
ipv6="$address6/$netmask"
# lookup IPv6, will be skipped if variable already set
if [ -z "$ipv6" ]; then
ipv6=$(ip -6 addr list $scope $device | grep -v " fd" | sed -n 's/.*inet6 \([0-9a-f:]\+\).*/\1/p' | head -n 1)
if [ -z "$ipv6" ]; then
echo "no IPv6 address found"
exit 1
fi
fi
ipv6="$ipv6/$netmask"
# update IPv4, if changed
if [ "$old4" = "$ipv4" ]; then
echo "IPv4 address unchanged"
else
# send IPv4 address to dynv6
echo -n "Updating IPv4: "
$bin "https://dynv6.com/api/update?zone=$zone&ipv4=$ipv4&token=$token"
echo ""
# save current address
# save current IPv4 address
echo $ipv4 > $file4
fi
@@ -82,12 +88,12 @@ fi
if [ "$old6" = "$ipv6" ]; then
echo "IPv6 address unchanged"
else
# send addresses to dynv6
# send IPv6 address to dynv6
echo -n "Updating IPv6: "
$bin "https://dynv6.com/api/update?zone=$zone&ipv6=$ipv6&token=$token"
echo ""
# save current address
# save current IPv6 address
echo $ipv6 > $file6
fi