Added bash & nushell versions

This commit is contained in:
ssn
2026-03-12 20:38:26 +01:00
parent 7a9cdd1166
commit 0b27cbb9de
2 changed files with 84 additions and 0 deletions

30
fixDNS.nu Normal file
View File

@@ -0,0 +1,30 @@
#!/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