2014-09-01 03:41:55 +00:00
|
|
|
#!/usr/bin/python
|
|
|
|
|
|
|
|
from dnsimple import DNSimple
|
|
|
|
from urllib import urlopen
|
|
|
|
from sys import stderr, exit
|
2014-09-01 15:38:47 +00:00
|
|
|
from apikey import API_EMAIL, API_KEY
|
2014-09-01 03:41:55 +00:00
|
|
|
|
|
|
|
domains = [{"domain": 'jshaver.net', "names": ["", "bc"]},
|
|
|
|
{"domain": 'theamazingshavers.com', "names": [""]}]
|
|
|
|
|
|
|
|
ttl = 600
|
|
|
|
|
2014-09-01 15:38:47 +00:00
|
|
|
dns = DNSimple(email=API_EMAIL, api_token=API_KEY)
|
2014-09-01 03:41:55 +00:00
|
|
|
|
|
|
|
response = urlopen("http://icanhazip.com/")
|
|
|
|
|
|
|
|
if response.getcode() != 200:
|
|
|
|
stderr.write("Non 200 status when retrieving ip! Status: {}".format(response.getcode()))
|
|
|
|
exit(-1)
|
|
|
|
|
|
|
|
ip = response.readline().rstrip()
|
|
|
|
|
|
|
|
def get_a_record(domain, name):
|
|
|
|
records = dns.records(domain)
|
2014-09-01 15:38:47 +00:00
|
|
|
for entry in records:
|
|
|
|
record = entry["record"]
|
2014-09-01 03:41:55 +00:00
|
|
|
if record["name"] is name and ["record_type"] is 'A':
|
|
|
|
return record
|
|
|
|
return None
|
|
|
|
|
|
|
|
def update_record(domain, name, record, ip):
|
2014-09-01 15:38:47 +00:00
|
|
|
new = {"name": name, "content": ip}
|
2014-09-01 03:41:55 +00:00
|
|
|
response = dns.update_record(domain, record["id"], new)
|
|
|
|
return response["record"]
|
|
|
|
|
|
|
|
def create_record(domain, name, ip):
|
2014-09-01 15:38:47 +00:00
|
|
|
new = {
|
2014-09-01 03:41:55 +00:00
|
|
|
"name": name,
|
2014-09-01 15:38:47 +00:00
|
|
|
"content": ip,
|
2014-09-01 03:41:55 +00:00
|
|
|
"record_type": "A",
|
2014-09-01 15:38:47 +00:00
|
|
|
"ttl": ttl}
|
|
|
|
print new
|
|
|
|
print domain
|
2014-09-01 03:41:55 +00:00
|
|
|
response = dns.add_record(domain, new)
|
|
|
|
return response["record"]
|
|
|
|
|
|
|
|
def update_domain_ip(domain, name, ip):
|
|
|
|
record = get_a_record(domain, name)
|
|
|
|
|
|
|
|
if record is None:
|
|
|
|
return create_record(domain, name, ip)
|
|
|
|
|
|
|
|
if record["content"] is ip:
|
|
|
|
return record
|
|
|
|
|
|
|
|
return update_record(record, name, ip)
|
|
|
|
|
|
|
|
def main(domainList, ip):
|
|
|
|
for domain in domainList:
|
|
|
|
for name in domain["names"]:
|
2014-09-01 15:38:47 +00:00
|
|
|
update_domain_ip(domain["domain"], name, ip)
|
2014-09-01 03:41:55 +00:00
|
|
|
|
|
|
|
main(domains, ip)
|
|
|
|
|