Created
July 29, 2022 06:43
-
-
Save shun-shobon/d5d7fd6ff697634b946584d4b0569496 to your computer and use it in GitHub Desktop.
DDNS Cloudflare for PowerShell
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
$dns_record = "<Write Here>" | |
$ttl = 120 | |
$cloudflare_zoneid = "<Write Here>" | |
$cloudflare_api_token = "<Write Here>" | |
$log_filename = "ddns.log" | |
$log_file = "$PSScriptRoot\$log_filename" | |
Write-Output "INFO [$(Get-Date)] 更新処理を開始します。" | Tee-Object $log_file -Append | |
$ipv4 = (Invoke-WebRequest -uri 'http://ifconfig.me/ip').Content.Trim() | |
if (!([bool]$ipv4)) { | |
Write-Output "ERROR [$(Get-Date)] IPv4アドレスの取得に失敗しました。" | Tee-Object $log_file -Append | |
exit 1 | |
} | |
$ipv6 = (Invoke-WebRequest -uri 'http://ifconfig.io/ip').Content.Trim() | |
if (!([bool]$ipv4)) { | |
Write-Output "ERROR [$(Get-Date)] IPv6アドレスの取得に失敗しました。" | Tee-Object $log_file -Append | |
exit 1 | |
} | |
$dns_record_ipv4 = (Resolve-DnsName -Name $dns_record -Type A -Server sharon.ns.cloudflare.com).IPAddress | |
$dns_record_ipv6 = (Resolve-DnsName -Name $dns_record -Type AAAA -Server sharon.ns.cloudflare.com).IPAddress | |
if ($ipv4 -eq $dns_record_ipv4) { | |
Write-Output "INFO [$(Get-Date)] IPv4アドレスが変更されていないため、更新は行いません。" | Tee-Object $log_file -Append | |
} else { | |
Write-Output "INFO [$(Get-Date)] IPv4アドレスが変更されているため、更新を行います。" | Tee-Object $log_file -Append | |
$record_info_ipv4_request = @{ | |
Uri = "https://api.cloudflare.com/client/v4/zones/$cloudflare_zoneid/dns_records?name=$dns_record&type=A" | |
Headers = @{ | |
"Authorization" = "Bearer $cloudflare_api_token" | |
"Content-Type" = "application/json" | |
} | |
} | |
$record_info_ipv4_response = Invoke-RestMethod @record_info_ipv4_request | |
if ($record_info_ipv4_response.success -ne "True") { | |
Write-Output "ERROR [$(Get-Date)] Aレコード情報の取得に失敗しました。" | Tee-Object $log_file -Append | |
exit 1 | |
} | |
$record_id_ipv4 = $record_info_ipv4_response.result.id | |
$update_dns_ipv4_request = @{ | |
Uri = "https://api.cloudflare.com/client/v4/zones/$cloudflare_zoneid/dns_records/$record_id_ipv4" | |
Method = "PUT" | |
Headers = @{ | |
"Authorization" = "Bearer $cloudflare_api_token" | |
"Content-Type" = "application/json" | |
} | |
Body = @{ | |
"type" = "A" | |
"name" = $dns_record | |
"content" = $ipv4 | |
"ttl" = $ttl | |
} | ConvertTo-Json | |
} | |
$update_dns_ipv4_response = Invoke-RestMethod @update_dns_ipv4_request | |
if ($update_dns_ipv4_response.success -ne "True") { | |
Write-Output "ERROR [$(Get-Date)] Aレコードの更新に失敗しました。" | Tee-Object $log_file -Append | |
exit 1 | |
} | |
Write-Output "INFO [$(Get-Date)] Aレコードの更新が完了しました。" | Tee-Object $log_file -Append | |
} | |
if ($ipv6 -eq $dns_record_ipv6) { | |
Write-Output "INFO [$(Get-Date)] IPv6アドレスが変更されていないため、更新は行いません。" | Tee-Object $log_file -Append | |
} else { | |
Write-Output "INFO [$(Get-Date)] IPv6アドレスが変更されているため、更新を行います。" | Tee-Object $log_file -Append | |
$record_info_ipv6_request = @{ | |
Uri = "https://api.cloudflare.com/client/v4/zones/$cloudflare_zoneid/dns_records?name=$dns_record&type=AAAA" | |
Headers = @{ | |
"Authorization" = "Bearer $cloudflare_api_token" | |
"Content-Type" = "application/json" | |
} | |
} | |
$record_info_ipv6_response = Invoke-RestMethod @record_info_ipv6_request | |
if ($record_info_ipv6_response.success -ne "True") { | |
Write-Output "ERROR [$(Get-Date)] Aレコード情報の取得に失敗しました。" | Tee-Object $log_file -Append | |
exit 1 | |
} | |
$record_id_ipv6 = $record_info_ipv6_response.result.id | |
$update_dns_ipv6_request = @{ | |
Uri = "https://api.cloudflare.com/client/v4/zones/$cloudflare_zoneid/dns_records/$record_id_ipv6" | |
Method = "PUT" | |
Headers = @{ | |
"Authorization" = "Bearer $cloudflare_api_token" | |
"Content-Type" = "application/json" | |
} | |
Body = @{ | |
"type" = "AAAA" | |
"name" = $dns_record | |
"content" = $ipv6 | |
"ttl" = $ttl | |
} | ConvertTo-Json | |
} | |
$update_dns_ipv6_response = Invoke-RestMethod @update_dns_ipv6_request | |
if ($update_dns_ipv6_response.success -ne "True") { | |
Write-Output "ERROR [$(Get-Date)] AAAAレコードの更新に失敗しました。" | Tee-Object $log_file -Append | |
exit 1 | |
} | |
Write-Output "INFO [$(Get-Date)] AAAAレコードの更新が完了しました。" | Tee-Object $log_file -Append | |
} | |
Write-Output "INFO [$(Get-Date)] 更新処理が完了しました。" | Tee-Object $log_file -Append |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment