Skip to content

Instantly share code, notes, and snippets.

@enriclluelles
Created February 22, 2016 23:21
Show Gist options
  • Save enriclluelles/570ae0b970078e67a8f6 to your computer and use it in GitHub Desktop.
Save enriclluelles/570ae0b970078e67a8f6 to your computer and use it in GitHub Desktop.
poor man's dyndns using route53
from __future__ import print_function
import boto3
import botocore
import requests
import re
import sys
ip = requests.get("https://api.ipify.org").text
if not re.match(r"^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$",ip):
print("Fail getting the ip", file=sys.stderr)
exit(1)
client = boto3.client('route53')
hzs = client.list_hosted_zones()
hz = next(h for h in hzs["HostedZones"] if h['Name'] == 'enriclluelles.com.')
hosted_zone_id = hz['Id']
rs = client.list_resource_record_sets(
HostedZoneId=hosted_zone_id)
record_set = next((r for r in rs["ResourceRecordSets"] if r["Name"] == "pi.enriclluelles.com."),None)
if record_set:
values = map(lambda r: r["Value"], record_set["ResourceRecords"])
if ip in values:
exit(0)
print("{} is not there, updating".format(ip))
status = client.change_resource_record_sets(
HostedZoneId=hosted_zone_id,
ChangeBatch={
"Comment": "update pi",
"Changes": [{
"Action":"UPSERT",
"ResourceRecordSet":{
"Name": "pi.enriclluelles.com.",
"Type":"A",
"TTL": 600,
"ResourceRecords":[{
"Value": ip
}]
}
}]
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment