Skip to content

Instantly share code, notes, and snippets.

@sasimpson
Created April 13, 2012 19:43
Show Gist options
  • Save sasimpson/2379569 to your computer and use it in GitHub Desktop.
Save sasimpson/2379569 to your computer and use it in GitHub Desktop.
tool to periodically update dns from a remote location
import requests
import json
from cloudauth import getauth
def update_record(domain_name=None, record_name=None):
token, dns_url = getauth('dnsextension:dns')
headers = {'x-auth-token': token, 'content-type': 'application/json'}
my_ip_resp = requests.get('http://automation.whatismyip.com/n09230945.asp')
if my_ip_resp.ok:
ip = my_ip_resp.text
else:
resp.raise_for_status()
resp = requests.get("%s/domains" % dns_url, headers=headers)
if resp.ok:
for d in json.loads(resp.text)['domains']:
if d['name'] == domain_name:
domain_id = d['id']
else:
resp.raise_for_status()
resp = requests.get("%s/domains/%s/records" % (dns_url, domain_id), headers=headers)
if resp.ok:
for r in json.loads(resp.text)['records']:
if r['name'] == record_name:
record_id = r['id']
else:
resp.raise_for_status()
if domain_id and record_id:
data = {
"name" : record_name,
"data" : ip,
"ttl" : 300
}
dns_up_uri = '%s/domains/%s/records/%s' % (dns_url, domain_id, record_id)
dns_up_resp = requests.put(dns_up_uri, data=json.dumps(data), headers=headers)
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser(description="dns updater")
parser.add_argument('-d', dest='domain', help="domain that your record is stored under")
parser.add_argument('-r', dest='record', help="record you wish to update")
args = parser.parse_args()
update_record(args.domain, args.record)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment