31 lines
1.5 KiB
Nu
31 lines
1.5 KiB
Nu
#!/usr/bin/env nu
|
|
#First stab at a nushell script, let's fix Cloudflare DNS
|
|
let timeStamp = date now | format date "%Y-%m-%dT%H:%M:%S"
|
|
let cfToken = "gbkanjmrKgCEqusC28anY3PNTrOdovurXTmbackA"
|
|
let myIp = http get https://api.ipify.org
|
|
let cfAPIBaseURI = "https://api.cloudflare.com/client/v4/"
|
|
let authHeader = {
|
|
Authorization: $"Bearer ($cfToken)"
|
|
}
|
|
let dnsZones = (http get $"($cfAPIBaseURI)zones" --headers $authHeader).result
|
|
#Iterate through the array of dnsZones
|
|
$dnsZones | each {
|
|
|zone|
|
|
#Get all the non-Tailscale A-records (those where the IP does not start with 100) that don't match $myIp
|
|
let badRecords = ((http get $"($cfAPIBaseURI)zones/($zone.id)/dns_records" --headers $authHeader).result | where type == "A" and content =~ "^(?!^100)" and content != $myIp)
|
|
$badRecords | each {
|
|
|badRecord|
|
|
#Update the DNS record to the correct IP
|
|
let recordBody = {
|
|
comment: $"($timeStamp) | Updated automatically from ($badRecord.content) to ($myIp)"
|
|
content: $"($myIp)"
|
|
}
|
|
let updateResult = (http patch $"($cfAPIBaseURI)zones/($zone.id)/dns_records/($badRecord.id)" $recordBody --headers $authHeader --content-type application/json)
|
|
if $updateResult.success {
|
|
print $"Successfully changed the DNS record for ($badRecord.name) from ($badRecord.content) to ($myIp)"
|
|
} else {
|
|
print $"Failed to change the DNS record for ($badRecord.name) from ($badRecord.content) to ($myIp)"
|
|
}
|
|
}
|
|
} | compact --empty #Don't output empty lists
|