Skip to content

Instantly share code, notes, and snippets.

@adamcstephens
Created September 28, 2023 12:38
Show Gist options
  • Save adamcstephens/69cc729b9ba86a5bb74cc51303a9a509 to your computer and use it in GitHub Desktop.
Save adamcstephens/69cc729b9ba86a5bb74cc51303a9a509 to your computer and use it in GitHub Desktop.
#!/usr/bin/env nu
def main [interface: string, hostname: string, --icanhazip: bool, --provider: string = "desec" ] {
let link = (ip --json link show dev $interface | from json)
if (($link.operstate | first) == "DOWN") {
print "Link not up. Exiting quietly"
exit 0
}
let addresses = if ($icanhazip) {
print "icanhazip addresses"
{
ipv4: (curl --silent https://icanhazip.com -4 | str trim),
ipv6: (curl --silent https://icanhazip.com -6 | str trim),
}
} else {
let ifaddr = (ip --json address show dev $interface
| from json
| get addr_info
| first
| filter { |a| $a.deprecated? != true and $a.mngtmpaddr? != true and $a.scope? != "link" })
print "Interface addresses"
print $ifaddr
{
ipv4: (($ifaddr | where family == "inet").local | first),
ipv6: (($ifaddr | where family == "inet6").local | first),
}
}
print $addresses
print $"Updating domain ($hostname) on provider ($provider)"
if ($provider == "he") {
print {
ipv4: (curl --ipv4 --silent $"https://dyn.dns.he.net/nic/update?hostname=($hostname)&password=($env.HE_DDNS_TOKEN)" | str trim)
ipv6: (curl --ipv6 --silent $"https://dyn.dns.he.net/nic/update?hostname=($hostname)&password=($env.HE_DDNS_TOKEN)" | str trim)
}
} else if ($provider == "desec") {
print {
desec: (curl --ipv4 --silent --header
$"Authorization: Token ($env.DESEC_DNS_TOKEN)" $"https://update.dedyn.io/?hostname=($hostname)&myipv4=($addresses.ipv4)&myipv6=($addresses.ipv6)"
| str trim)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment