Created
July 14, 2023 16:18
-
-
Save gerard/efb29401ad33a40fa8226dd7afc02926 to your computer and use it in GitHub Desktop.
DNS record update via godaddy API
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
#!/usr/bin/env python3 | |
import os | |
import json | |
import argparse | |
import subprocess | |
import requests | |
def get_secret(sname): | |
fname = os.path.join(os.sep, "etc", "godaddy", sname) | |
return subprocess.check_output(["cat", fname]).decode("ascii").strip() | |
def godaddy_api(path): | |
return "https://api.godaddy.com/{}".format(path) | |
def get_publicip(): | |
myip_provider = get_secret("myip_provider") | |
ip = requests.get(myip_provider).text.strip() | |
# Sanity check | |
ip_components = [int(x) for x in ip.split(".")] | |
assert len(ip_components) == 4 | |
for ipc in ip_components: | |
assert int(ipc) < 256 | |
return ip | |
def main(): | |
parser = argparse.ArgumentParser() | |
parser.add_argument('-f', '--force', action='store_true') | |
args = parser.parse_args() | |
s = requests.Session() | |
s.headers.update({ | |
"Authorization": "sso-key {}:{}".format( | |
get_secret("apikey"), | |
get_secret("apisecret"), | |
) | |
}) | |
own_ip = get_publicip() | |
domain = get_secret("domain") | |
rec = get_secret("ddnsrec") | |
r = s.get(godaddy_api( | |
f"v1/domains/{domain}/records/A/{rec}" | |
)) | |
rec_ip = r.json()[0]["data"] | |
if rec_ip != own_ip or args.force: | |
print(f"Updating {rec_ip} => {own_ip}") | |
r = s.put( | |
godaddy_api(f"v1/domains/{domain}/records/A/{rec}"), | |
headers={"Content-Type": "application/json"}, | |
data=json.dumps([{"data": own_ip}]) | |
) | |
if r.status_code != 200: | |
print(json.dumps(r.json(), indent=4)) | |
r.raise_for_status() | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment