Files
wdyndns/awsdyndns/wrapper.py
2018-11-20 01:19:50 +00:00

48 lines
1.6 KiB
Python
Executable File

#!/usr/bin/env python3
import json
from urllib.request import urlopen, Request
import argparse
from datetime import datetime
from subprocess import check_call
def run():
parser = argparse.ArgumentParser()
parser.add_argument('-z', "--hosted-zone", metavar='HOSTED_ZONE_ID', required=True,
help="Specify the hosted zone id")
parser.add_argument('-d', "--domain", metavar='DOMAIN_NAME', required=True,
help="Specify domain name")
parser.add_argument('-t', "--type", metavar='DNS_TYPE', default='A',
help="Specify DNS type")
parser.add_argument('-l', "--ttl", metavar='TTL_S', type=int, default=300,
help="Specify time-to-live in seconds")
args = parser.parse_args()
url = "http://v4.ipv6-test.com/api/myip.php"
request = Request(url)
with urlopen(request) as request:
public_ip = request.read().decode()
date = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
params = {
'Comment': f'Auto update by awsdyndns on {date}',
'Changes': [
{
'Action': 'UPSERT',
'ResourceRecordSet': {
'ResourceRecords': [{'Value': public_ip}],
'Name': args.domain,
'Type': args.type,
'TTL': args.ttl
}
}
]
}
check_call(['aws', 'route53', 'change-resource-record-sets', '--hosted-zone-id', args.hosted_zone, '--change-batch', json.dumps(params)])
if __name__ == "__main__":
run()