默认分类

使用cloudflare 接管域名并使用ddns

ipv6

用于保存主机名的文件路径

$hostnameFilePath = "C:UsersPublichostname.txt"

检查是否存在保存的主机名

if (Test-Path $hostnameFilePath) {

$hostname = Get-Content $hostnameFilePath

} else {

# 如果文件不存在,提示用户输入主机名
$hostname = Read-Host "请输入 DNS 记录的主机名"

# 将主机名保存到文件
$hostname | Set-Content $hostnameFilePath

}

拼接域名后缀

$fullDomain = $hostname + ".moonnn.top"

输出完整的域名

Write-Host "完整的域名是: $fullDomain"

在后续的脚本中,你可以使用 $fullDomain 变量来获取完整的域名

$interfaceAlias = "vEthernet (ax210)"

$ipv6Address = Get-NetIPAddress | Where-Object {

$_.InterfaceAlias -eq $interfaceAlias -and
$_.AddressFamily -eq "IPv6" -and
$_.PrefixOrigin -ne "WellKnown" -and
$_.SuffixOrigin -ne "LinkLayerAddress" -and
$_.IPAddress -notlike "fe80::*" -and
$_.IPAddress -notlike "ff*"

} | Select-Object -First 1

if ($ipv6Address) {

Write-Host "IPv6地址: $($ipv6Address.IPAddress)"

} else {

Write-Host "没有找到符合条件的IPv6地址。"

}

设置 Cloudflare API 认证信息

$apiKey = "a67b18ee666f49995812c62150de3d962018fe5"
$email = "skywikia@gmail.com"
$zoneId = "947ef2089e282467c3f221d9b32279a4ec3"

设置要更新的 DNS 记录信息

$recordType = "AAAA" # IPv6 记录类型
$name = $fullDomain # DNS 记录的主机名
$newIPv6Address = $($ipv6Address.IPAddress) # 新的 IPv6 地址
$ttl = 3600 # TTL (Time to Live),以秒为单位
$proxied = $false # 是否启用 Cloudflare 代理

构建 API 请求的 JSON 数据

$recordData = @{

"type" = $recordType
"name" = $name
"content" = $newIPv6Address  # 更新为新的 IPv6 地址
"ttl" = $ttl
"proxied" = $proxied

} | ConvertTo-Json

检查是否已存在相同名称和类型的 DNS 记录

$headers = @{

"X-Auth-Email" = $email
"X-Auth-Key" = $apiKey

}

$existingRecord = Invoke-RestMethod -Uri "https://api.cloudflare.com/client/v4/zones/$zoneId/dns_records?type=$recordType&name=$name" -Method Get -Headers $headers

如果存在相同记录,则执行更新操作

if ($existingRecord.result.Count -gt 0) {

$existingRecordId = $existingRecord.result[0].id
Invoke-RestMethod -Uri "https://api.cloudflare.com/client/v4/zones/$zoneId/dns_records/$existingRecordId" -Method Put -Headers $headers -Body $recordData
Write-Host "IPv6 DNS 记录已更新。"

}

否则,执行创建操作

else {

Invoke-RestMethod -Uri "https://api.cloudflare.com/client/v4/zones/$zoneId/dns_records" -Method Post -Headers $headers -Body $recordData
Write-Host "IPv6 DNS 记录已创建。"

}

将脚本自身复制到"C:UsersPublic"

获取当前脚本的文件路径

$scriptPath = $MyInvocation.MyCommand.Path

指定目标目录

$destinationDirectory = "C:UsersPublic"

构建目标文件的完整路径

$destinationPath = Join-Path -Path $destinationDirectory -ChildPath (Split-Path -Leaf $scriptPath)

检查目标文件是否已存在

if (-not (Test-Path -Path $destinationPath)) {

# 复制脚本到目标目录
Copy-Item -Path $scriptPath -Destination $destinationPath -Force
Write-Host "脚本已成功复制到 $destinationPath。"

} else {

Write-Host "目标文件已存在,不需要复制。"

}

创建计划任务

设置计划任务名称和脚本路径

$taskName = "cf-ddns6"
$scriptPath = "C:UsersPubliccf-ddns-ipv6.ps1"

检查是否存在具有相同名称的计划任务

$existingTask = Get-ScheduledTask -TaskName $taskName -ErrorAction SilentlyContinue

if ($existingTask -eq $null) {

# 如果不存在同名任务,创建新的计划任务
$username = $env:USERNAME

Write-Host "当前用户名是: $username"

$password = Read-Host "请输入用户密码"


schtasks /create /tn $taskName /tr "powershell.exe -c $scriptPath" /sc daily /st 00:00 /ri 5 /du 24:00 /ru $username /rp $password

# 启动计划任务(可以手动启动)
Start-ScheduledTask -TaskName $taskName

} else {

Write-Host "同名的计划任务已存在,不创建新任务。"

}

ipv4

用于保存主机名的文件路径

$hostnameFilePath = "C:UsersPublichostname.txt"

检查是否存在保存的主机名

if (Test-Path $hostnameFilePath) {

$hostname = Get-Content $hostnameFilePath

} else {

# 如果文件不存在,提示用户输入主机名
$hostname = Read-Host "请输入 DNS 记录的主机名"

# 将主机名保存到文件
$hostname | Set-Content $hostnameFilePath

}

拼接域名后缀

$fullDomain = $hostname + ".moonnn.top"

输出完整的域名

Write-Host "完整的域名是: $fullDomain"

在后续的脚本中,你可以使用 $fullDomain 变量来获取完整的域名

$interfaceAlias = "vEthernet (mes)"

$ipv4Addresses = Get-NetIPAddress | Where-Object { $_.InterfaceAlias -eq $interfaceAlias -and $_.AddressFamily -eq "IPv4" }

foreach ($ipv4Address in $ipv4Addresses) {

Write-Host "IPv4地址: $($ipv4Address.IPAddress)"

}

设置 Cloudflare API 认证信息

$apiKey = "a67b18ee666f499958ee1c62150de3d96208fe5"
$email = "skywikia@gmail.com"
$zoneId = "947ef2089e282467c3fee1d9b3279a4ec3"

设置要创建/更新的 DNS 记录信息

$recordType = "A" # DNS 记录类型(例如:A、CNAME、MX 等)
$name = $fullDomain # DNS 记录的主机名
$content = $ipv4Address.IPAddress # DNS 记录的内容
$ttl = 3600 # TTL (Time to Live),以秒为单位
$proxied = $false # 是否启用 Cloudflare 代理

构建 API 请求的 JSON 数据

$recordData = @{

"type" = $recordType
"name" = $name
"content" = $content
"ttl" = $ttl
"proxied" = $proxied

} | ConvertTo-Json

检查是否已存在相同名称和类型的 DNS 记录

$headers = @{

"X-Auth-Email" = $email
"X-Auth-Key" = $apiKey

}

$existingRecord = Invoke-RestMethod -Uri "https://api.cloudflare.com/client/v4/zones/$zoneId/dns_records?type=$recordType&name=$name" -Method Get -Headers $headers

如果存在相同记录,则执行更新操作

if ($existingRecord.result.Count -gt 0) {

$existingRecordId = $existingRecord.result[0].id
Invoke-RestMethod -Uri "https://api.cloudflare.com/client/v4/zones/$zoneId/dns_records/$existingRecordId" -Method Put -Headers $headers -Body $recordData
Write-Host "DNS 记录已更新。"

}

否则,执行创建操作

else {

Invoke-RestMethod -Uri "https://api.cloudflare.com/client/v4/zones/$zoneId/dns_records" -Method Post -Headers $headers -Body $recordData
Write-Host "DNS 记录已创建。"

}

将脚本自身复制到"C:UsersPublic"

获取当前脚本的文件路径

$scriptPath = $MyInvocation.MyCommand.Path

指定目标目录

$destinationDirectory = "C:UsersPublic"

构建目标文件的完整路径

$destinationPath = Join-Path -Path $destinationDirectory -ChildPath (Split-Path -Leaf $scriptPath)

检查目标文件是否已存在

if (-not (Test-Path -Path $destinationPath)) {

# 复制脚本到目标目录
Copy-Item -Path $scriptPath -Destination $destinationPath -Force
Write-Host "脚本已成功复制到 $destinationPath。"

} else {

Write-Host "目标文件已存在,不需要复制。"

}

创建计划任务

设置计划任务名称和脚本路径

$taskName = "cf-ddns"
$scriptPath = "C:UsersPubliccf-ddns.ps1"

检查是否存在具有相同名称的计划任务

$existingTask = Get-ScheduledTask -TaskName $taskName -ErrorAction SilentlyContinue

if ($existingTask -eq $null) {

# 如果不存在同名任务,创建新的计划任务
$username = $env:USERNAME

Write-Host "当前用户名是: $username"

$password = Read-Host "请输入用户密码"


schtasks /create /tn $taskName /tr "powershell.exe -c $scriptPath" /sc daily /st 00:00 /ri 5 /du 24:00 /ru $username /rp $password

# 启动计划任务(可以手动启动)
Start-ScheduledTask -TaskName $taskName

} else {

Write-Host "同名的计划任务已存在,不创建新任务。"

}

pause

回复

This is just a placeholder img.