Created
February 6, 2014 11:49
-
-
Save jamesoff/8842676 to your computer and use it in GitHub Desktop.
Roll your own dynamic DNS with Route53
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/python | |
""" | |
Uses reflect.php on sakaki to figure out external IP and then requests Amazon updates the record for home.jamesoff.net with that | |
JMS | |
20131019 | |
""" | |
HOSTED_ZONE_ID = "Z1BQWQ5LVT6ZAX" | |
DNS_NAME = "home.jamesoff.net" | |
import sys | |
import requests | |
import boto | |
import socket | |
try: | |
r = requests.get("http://sakaki.jamesoff.net/~james/reflect.php") | |
except Exception, e: | |
print "Error loading data from reflect service" | |
print e | |
sys.exit(1) | |
if r.status_code != 200: | |
print "Did not get status code 200 from remote service" | |
sys.exit(1) | |
try: | |
if r.json()["error"]: | |
print "Got error from remote service" | |
sys.exit(1) | |
except: | |
print "Got response 200 but had error parsing JSON" | |
try: | |
current_dns = socket.gethostbyname(DNS_NAME) | |
if current_dns == r.json()["your_ip"]: | |
#print "Current entry matches (%s)" % current_dns | |
sys.exit(0) | |
except socket.gaierror, e: | |
if e.errno == 8: | |
print "No record currently exists for %s" % DNS_NAME | |
except Exception, e: | |
print "Error lookup up current IP" | |
print e | |
sys.exit(1) | |
try: | |
route53 = boto.connect_route53() | |
records = route53.get_all_rrsets(HOSTED_ZONE_ID) | |
change1 = records.add_change("DELETE", DNS_NAME, "A") | |
change1.add_value(current_dns) | |
change2 = records.add_change("CREATE", DNS_NAME, "A") | |
change2.add_value(r.json()["your_ip"]) | |
records.commit() | |
except Exception, e: | |
print "Error changing record at Amazon" | |
print e | |
sys.exit(2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment