Created
April 10, 2013 13:22
-
-
Save EmilHernvall/5354570 to your computer and use it in GitHub Desktop.
Script for dynamically updating a route53 record when your external ip changes.
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 | |
import boto.route53 | |
import os | |
import sys | |
import urllib2 | |
import time | |
def get_zone_id(r53, zone_name): | |
zone_result = r53.get_hosted_zone_by_name(zone_name) | |
if not zone_result: | |
print "%s not found" % (zone_name,) | |
return None | |
zone = zone_result["GetHostedZoneResponse"]["HostedZone"] | |
zone_id = zone["Id"].split("/")[2] | |
return zone_id | |
def get_current_record(r53, zone_id, host): | |
records = r53.get_all_rrsets(zone_id, name=host) | |
records = [record for record in records if record.name.startswith(host)] | |
if len(records) == 0: | |
print "no record found" | |
return None | |
record = records[0] | |
if record.type != "A": | |
print "not an A record" | |
return None | |
return record | |
def get_external_ip(): | |
current_ip = urllib2.urlopen("http://znaptag.com/~emil/ip.php").read() | |
return current_ip | |
def monitor(r53, zone_id, host): | |
while True: | |
try: | |
record = get_current_record(r53, zone_id, host) | |
if record: | |
record_ip = record.resource_records[0] | |
else: | |
record_ip = "not set" | |
print "no record currently exists for " + host | |
current_ip = get_external_ip() | |
if record_ip != current_ip: | |
print "ip missmatch, changing" | |
print "current ip: " + current_ip | |
print "record ip: " + record_ip | |
change = boto.route53.record.ResourceRecordSets(connection=r53, | |
hosted_zone_id=zone_id) | |
if record: | |
change.add_change_record("DELETE", record) | |
change.add_change_record("CREATE", | |
boto.route53.record.Record(name=host, | |
type="A", | |
resource_records=[current_ip], | |
alias_hosted_zone_id=zone_id)) | |
change.commit() | |
print "record changed" | |
except urllib2.URLError as e: | |
print repr(e) | |
except boto.route53.exception.DNSServerError as e: | |
print repr(e) | |
time.sleep(600) | |
if __name__ == "__main__": | |
access_key = os.environ["AMAZON_ACCESS_KEY_ID"] | |
secret_key = os.environ["AMAZON_SECRET_ACCESS_KEY"] | |
if len(sys.argv) != 2: | |
print "usage: %s host" % (sys.argv,) | |
sys.exit(0) | |
host = sys.argv[1] | |
zone_name = ".".join(host.split(".")[-2:]) | |
r53 = boto.route53.connection.Route53Connection(aws_access_key_id=access_key, | |
aws_secret_access_key=secret_key) | |
zone_id = get_zone_id(r53, zone_name) | |
if not zone_id: | |
print "zone not found: " + zone_name | |
sys.exit(1) | |
print "zone id: " + zone_id | |
print "monitoring..." | |
try: | |
monitor(r53, zone_id, host) | |
except KeyboardInterrupt: pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment