forked from SeanPONeil/route53-dyndns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
r53dyndns.py
executable file
·199 lines (146 loc) · 5.69 KB
/
r53dyndns.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#! /usr/bin/env python3
"""Updates a Route53 hosted A record with the current ip of the system.
"""
import argparse
import logging
import time
import dns
import dns.resolver
import os
import socket
import boto3
def get_ip_of_hostname(hostname):
ip = socket.gethostbyname(hostname)
logging.info(f'IP Address of ${hostname}: ${ip}')
return ip
def get_current_local_ip():
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(("8.8.8.8", 80))
current_ip = s.getsockname()[0]
s.close()
logging.info(f'Current local IP address: {current_ip}')
return current_ip
def get_current_public_ip():
resolver = dns.resolver.Resolver()
resolver.nameservers = [socket.gethostbyname('resolver1.opendns.com')]
for rdata in resolver.query('myip.opendns.com', 'A'):
current_ip = str(rdata)
logging.info(f'Current IP address: {current_ip}')
return current_ip
def get_zone(zone_to_update):
client = boto3.client('route53')
zone = client.list_hosted_zones_by_name(
DNSName=zone_to_update,
MaxItems='1'
)
hosted_zone = zone['HostedZones']
for z in hosted_zone:
hosted_zone_id = z['Id'].split('/')[2]
logging.info(f'Hosted Zone ID: {hosted_zone_id}')
return hosted_zone_id
def get_record_value(hosted_zone_id, record_to_update, record_type):
client = boto3.client('route53')
record_set = client.list_resource_record_sets(
HostedZoneId=hosted_zone_id,
StartRecordName=record_to_update,
StartRecordType=record_type,
MaxItems='1'
)
record = record_set['ResourceRecordSets']
record_ip = None
for v in record:
record_ip = v['ResourceRecords'][0]['Value']
logging.info(f'Current record IP Address: {record_ip}')
return record_ip
def update_record(hosted_zone_id, record_to_update, record_type, record_value):
def get_change_status(change_id):
client = boto3.client('route53')
change_status = client.get_change(
Id=change_id
)
status = change_status['ChangeInfo']['Status']
logging.info(f'Change Status: {status}')
return status
client = boto3.client('route53')
change = client.change_resource_record_sets(
HostedZoneId=hosted_zone_id,
ChangeBatch={
'Comment': 'Updated by r53dns.py',
'Changes': [
{
'Action': 'UPSERT',
'ResourceRecordSet': {
'Name': record_to_update,
'Type': record_type,
'TTL': 60,
'ResourceRecords': [
{
'Value': record_value
}
]
}
}
]
}
)
change_id = change['ChangeInfo']['Id'].split('/')[2]
logging.info(f'Change ID: {change_id}')
status = status = get_change_status(change_id)
if status == 'PENDING':
logging.info('Waiting for change to complete...')
while status == 'PENDING':
time.sleep(2)
status = get_change_status(change_id)
if status == 'INSYNC':
logging.info('Change complete')
return status
def main():
parser = argparse.ArgumentParser(
description='Update a Route53 hosted A record with with current external IP address of the system.')
parser.add_argument('-r', '--record', help='specify the DNS A record to update')
parser.add_argument('-v', '--verbose', help='enable verbose output',
action="store_true")
args = parser.parse_args()
route53_record_ip = os.getenv('ROUTE53_DOMAIN_A_RECORD_IP')
route53_cname_record = os.getenv('ROUTE53_DOMAIN_CNAME_RECORD')
route53_record_ip_local = os.getenv('ROUTE53_DOMAIN_A_RECORD_IP_LOCAL')
if args.record is None:
logging.error('No record specified')
parser.print_help()
exit(-1)
if args.verbose:
logging.basicConfig(
level=logging.INFO,
)
logging.info('Verbose output enabled')
record_to_update = args.record
logging.info(f'Updating A record: {record_to_update}')
zone_to_update = '.'.join(record_to_update.split('.')[-2:])
logging.info(f'Route53 zone: {zone_to_update}')
if route53_record_ip:
print(f'Updating A record with {route53_record_ip}')
current_ip = route53_record_ip
elif route53_record_ip_local:
print('Getting current local IP address')
current_ip = get_current_local_ip()
else:
print('Getting current public IP address')
current_ip = get_current_public_ip()
hosted_zone_id = get_zone(zone_to_update)
record_ip = get_record_value(hosted_zone_id, record_to_update, 'A')
if current_ip != record_ip:
logging.info(f'Updating A record: {record_to_update} with {current_ip}')
status = update_record(hosted_zone_id, record_to_update, 'A', current_ip)
if status == 'INSYNC':
print(
f'Updated A record {record_to_update} in hosted zone {zone_to_update} ({hosted_zone_id}) from '
f'{record_ip} to {current_ip}')
if route53_cname_record and record_to_update != get_record_value(hosted_zone_id, record_to_update, 'CNAME'):
logging.info(f'Updating CNAME record: {route53_cname_record} with {record_to_update}')
status = update_record(hosted_zone_id, route53_cname_record, 'CNAME', record_to_update)
if status == 'INSYNC':
print(
f'Updated CNAME record {record_to_update} in hosted zone '
f'{zone_to_update} ({hosted_zone_id}) to {record_to_update}')
if __name__ == '__main__':
main()