From bbdc84261cf63055bb6b575b0a0a2da11036ab67 Mon Sep 17 00:00:00 2001 From: Andrew Stowell Date: Wed, 20 Sep 2023 20:58:19 -0400 Subject: [PATCH] Updated Dynv6 PowerShell script to more closely match bash shell script. --- UpdateDynv6.ps1 => Dynv6.ps1 | 53 +++++++++++++++++++++++++----------- dynv6.sh | 8 +++--- 2 files changed, 41 insertions(+), 20 deletions(-) rename UpdateDynv6.ps1 => Dynv6.ps1 (66%) diff --git a/UpdateDynv6.ps1 b/Dynv6.ps1 similarity index 66% rename from UpdateDynv6.ps1 rename to Dynv6.ps1 index 9fcb26d..2c48721 100644 --- a/UpdateDynv6.ps1 +++ b/Dynv6.ps1 @@ -55,39 +55,60 @@ param( - [string]$zone = $(throw "-zone is required"), - [string]$token = $(throw "-token is required"), - [string]$logfile = "$env:USERPROFILE\\DDNS_UPDATE_LOG", - [switch]$isIPV6 = $false - ) + [string] $zone = $(throw "-zone is required"), + [string] $token = $(throw "-token is required"), + [string] $netmask = "128", + [string] $logfile = "$env:USERPROFILE\\.dynv6.log" +) -$lookup4 =[System.Uri]'https://ipinfo.io/ip' -$lookup6 =[System.Uri]'https://v6.ipinfo.io/ip' -# google can return either IPv4 or IPv6 'https://domains.google.com/checkip' +$file4 = "$env:USERPROFILE\\.dynv6.addr4" +$file6 = "$env:USERPROFILE\\.dynv6.addr6" -$update4 =[System.Uri]'https://dynv6.com/api/update' -$update6 =[System.Uri]'https://dynv6.com/api/update' +if (Test-Path -Path $file4) { + $old4 = Get-Content -Path $file4 +} else { + $old4 = $null +} +if (Test-Path -Path $file6) { + $old6 = Get-Content -Path $file6 +} else { + $old6 = $null +} + +$lookup4 = [System.Uri]'https://ipinfo.io/ip' +$lookup6 = [System.Uri]'https://v6.ipinfo.io/ip' + +$update4 = [System.Uri]'https://dynv6.com/api/update' +$update6 = [System.Uri]'https://dynv6.com/api/update' Get-Date | Tee-Object -FilePath $logfile -Append | Out-Host -if($isIPV6 -eq $false){ - $current4 = Invoke-RestMethod -Method Get -Uri $lookup4 +$current4 = Invoke-RestMethod -Method Get -Uri $lookup4 +$current6 = Invoke-RestMethod -Method Get -Uri $lookup6 +if ($old4 -eq $current4) { + Tee-Object -FilePath $logfile -Append -InputObject "IPv4 address unchanged" | Out-Host +} else { + Tee-Object -FilePath $logfile -Append -InputObject "Updating IPv4" | Out-Host $body = @{ zone = $zone token = $token ipv4 = $current4 } - Invoke-RestMethod -Method Get -Uri $update4 -Body $body | Tee-Object -FilePath $lotfile -Append | Out-Host + Invoke-RestMethod -Method Get -Uri $update4 -Body $body | Tee-Object -FilePath $logfile -Append | Out-Host + Out-File -FilePath $file4 -InputObject $current4 } -else{ - $current6 = Invoke-RestMethod -Method Get -Uri $lookup6 +if ($old6 -eq $current6) { + Tee-Object -FilePath $logfile -Append -InputObject "IPv6 address unchanged" | Out-Host +} else { + Tee-Object -FilePath $logfile -Append -InputObject "Updating IPv6" | Out-Host $body = @{ zone = $zone token = $token - ipv6 = "$current6/128" + ipv6 = "$current6/$netmask" } Invoke-RestMethod -Method Get -Uri $update6 -Body $body | Tee-Object -FilePath $logfile -Append | Out-Host + Out-File -FilePath $file6 -InputObject $current6 } diff --git a/dynv6.sh b/dynv6.sh index daf6898..2f5d3bd 100755 --- a/dynv6.sh +++ b/dynv6.sh @@ -1,6 +1,6 @@ #!/bin/sh -hostname=$1 +zone=$1 device=$2 scope=$3 @@ -11,7 +11,7 @@ file6="$HOME/.dynv6.addr6" [ -e $file6 ] && old6=`cat $file6` # ensure we have required information -if [ -z "$hostname" -o -z "$token" ]; then +if [ -z "$zone" -o -z "$token" ]; then echo "Usage: token= [netmask=64] $0 your-name.dynv6.net [device] [scope]" exit 1 fi @@ -71,7 +71,7 @@ if [ "$old4" = "$ipv4" ]; then echo "IPv4 address unchanged" else echo -n "Updating IPv4: " - $bin "https://dynv6.com/api/update?hostname=$hostname&ipv4=$ipv4&token=$token" + $bin "https://dynv6.com/api/update?zone=$zone&ipv4=$ipv4&token=$token" echo "" # save current address @@ -84,7 +84,7 @@ if [ "$old6" = "$ipv6" ]; then else # send addresses to dynv6 echo -n "Updating IPv6: " - $bin "https://dynv6.com/api/update?hostname=$hostname&ipv6=$ipv6&token=$token" + $bin "https://dynv6.com/api/update?zone=$zone&ipv6=$ipv6&token=$token" echo "" # save current address