-
Notifications
You must be signed in to change notification settings - Fork 0
/
dyn-dnscmd.py
369 lines (323 loc) · 12.3 KB
/
dyn-dnscmd.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
#!/usr/bin/env python3
#
# Command line tool for editing external DNS records via DynDNS API.
# Author: Sergey Polyakov <[email protected]>
# Documentation: https://help.dyn.com/understanding-works-api/
#
import sys
import cmd
import getpass
import requests
DynectURL = 'https://api.dynect.net'
URI = {
'login': '/REST/Session/?customer_name={0}&user_name={1}&password={2}',
'logout': '/REST/Session/',
'record': '/REST/{0}Record/{1}/{2}/{3}',
'zone_publish': '/REST/Zone/{0}?publish=true',
'zones': '/REST/Zone'
}
rdata_map = {
'CNAME': 'cname',
'A': 'address',
'AAAA': 'address',
'PTR': 'ptrdname',
'MX': 'exchange',
'TXT': 'txtdata',
'NS': 'nsdname'
}
class DynectSession:
# headers dict holds headers for all HTTP requests
headers = {'Auth-Token': None, 'Content-Type': 'application/json'}
# Zone is string like 'example.org'
Zone = None
def __init__(self, **kwargs):
# check if auth_token already passed as argument:
if 'auth_token' in kwargs.keys():
self.headers['Auth-Token'] = kwargs['auth_token']
def __enter__(self):
return self
def __exit__(self, typ, val, tb):
if self.headers['Auth-Token']:
self.Logout()
# get auth token
def Login(self, **kwargs):
customer_name = kwargs['customer_name']
user_name = kwargs['user_name']
password = kwargs['password']
url = DynectURL + URI['login'].format(customer_name, user_name, password)
resp = requests.post(url, headers=self.headers).json()
if resp['status'] != 'success':
raise Exception('Login failed: ' + str(resp['msgs']))
self.headers['Auth-Token'] = resp['data']['token']
# logout and forget auth token
def Logout(self):
url = DynectURL + URI['logout']
resp = requests.delete(url, headers=self.headers).json()
if resp['status'] != 'success':
raise Exception('Logout failed: ' + str(resp['msgs']))
else:
self.headers['Auth-Token'] = None
# Returns current auth-token
def Token(self):
return self.headers['Auth-Token']
# get list of records with the same name and rtype
def GetRecordSet(self, record, rtype):
rtype = rtype.upper()
# get record URIs in format /REST/<rtype>Record/<zone>/<fqdn>/<id>
url = DynectURL + URI['record'].format(rtype, self.Zone, record, '')
resp = requests.get(url, headers=self.headers).json()
recordURIs = [ uri for uri in resp['data'] ]
# get record details
records = []
for uri in recordURIs:
url = DynectURL + uri
resp = requests.get(url, headers=self.headers).json()
records.append(resp['data'])
return records
# like GetRecordSet, but throws exception if multiple records found, returns single record
def GetRecord(self, record, rtype):
rtype = rtype.upper()
# get record URIs in format /REST/<rtype>Record/<zone>/<fqdn>/<id>
url = DynectURL + URI['record'].format(rtype, self.Zone, record, '')
resp = requests.get(url, headers=self.headers).json()
if len(resp['data']) > 1:
raise Exception('Multiple record IDs returned for {0}.'.format(record))
# get record details
url = DynectURL + resp['data'][0]
resp = requests.get(url, headers=self.headers).json()
return resp['data']
# this doesn't through exception if multiple records found, but requires exact rdata to be specified
def GetRecordID(self, record, rtype, rdata):
records = self.GetRecordSet(record, rtype)
try:
record_id = [ str(r['record_id']) for r in records if r['rdata'][rdata_map[rtype]].strip('.') == rdata.strip('.') ][0]
except:
raise Exception('Record not found.')
return record_id
def CreateRecord(self, record, rtype, rdata, **kwargs):
url = DynectURL + URI['record'].format(rtype, self.Zone, record, '')
body = {'rdata':{rdata_map[rtype]:rdata}}
if 'ttl' in kwargs.keys():
body.update({'ttl':kwargs.pop('ttl')})
body['rdata'].update(kwargs)
resp = requests.post(url, headers=self.headers, json=body).json()
if resp['status'] != 'success':
raise Exception('Create failure: ' + str(resp['msgs']))
def UpdateRecord(self, record, rtype, rdata, **kwargs):
record_id = self.GetRecord(record, rtype)['record_id']
url = DynectURL + URI['record'].format(rtype, self.Zone, record, record_id)
body = {'rdata':{rdata_map[rtype]:rdata}}
if 'ttl' in kwargs.keys():
body.update({'ttl':kwargs.pop('ttl')})
body['rdata'].update(kwargs)
resp = requests.put(url, headers=self.headers, json=body).json()
if resp['status'] != 'success':
raise Exception('Update failure: ' + str(resp['msgs']))
def DeleteRecord(self, record, rtype, rdata):
record_id = self.GetRecordID(record, rtype, rdata)
url = DynectURL + URI['record'].format(rtype, self.Zone, record, record_id)
resp = requests.delete(url, headers=self.headers).json()
if resp['status'] != 'success':
raise Exception('Update failure: ' + str(resp['msgs']))
def GetZones(self):
url = DynectURL + URI['zones']
resp = requests.get(url, headers=self.headers).json()
if resp['status'] != 'success':
raise Exception('Update failure: ' + str(resp['msgs']))
zones = [ zone.split('/')[-2] for zone in resp['data'] ]
return zones
# always publish your changes!
def Publish(self):
url = DynectURL + URI['zone_publish'].format(self.Zone)
resp = requests.put(url, headers=self.headers).json()
if resp['status'] != 'success':
raise Exception('Publish failure: ' + str(resp['msgs']))
# global dynect session object
dyn = DynectSession()
# set of zones that changes were made in
affected_zones = set()
# customer name is usually always the same
default_customer_name = 'ringcentral'
# this just returns zone which the record belongs to
def get_zone(fqdn, zones):
nodes = fqdn.split('.')
for i in list(range(len(nodes))):
zone = '.'.join(nodes[i:])
if zone in zones:
return zone
# if no zone found
return ''
# parses csvline into plain list of arguments
def parse_args(csvline):
separator = ' '
escape_substitutor = '%!:SPACE&^*'
csvline = csvline.replace('\\'+separator, escape_substitutor)
args = [ a for a in csvline.split(separator) if a != '' ]
return [ a.replace(escape_substitutor, ' ') for a in args ]
# command line interface class
class Cli(cmd.Cmd):
def __init__(self):
cmd.Cmd.__init__(self)
self.prompt = '> '
self.ruler = ''
self.intro = 'For help type \'help\'.'
self.doc_header = "Interactive command line tool for editing external DNS records via DynDNS API.\nTo get help on a command type 'help <command>'. To exit type 'exit'\nAvailable commands:"
self.managed_zones = list()
def emptyline(self):
# do nothing if empty line is input
pass
def preloop(self):
# download manged zones
if not self.managed_zones:
try:
self.managed_zones = dyn.GetZones()
except Exception as E:
print(E)
def do_get(self, line):
# parse args
args = parse_args(line)
if len(args) != 2:
print("Bad number of arguments. See 'help get'.")
return
fqdn = args[0].strip('.')
rtype = args[1].upper()
# get result
zone = get_zone(fqdn, self.managed_zones)
if zone == '':
print('Zone not found for '+fqdn)
return
else:
dyn.Zone = zone
try:
print(dyn.GetRecordSet(fqdn, rtype))
except Exception as E:
print(E)
def do_add(self, line):
# parse args
args = parse_args(line)
if len(args) != 4:
print("Bad number of arguments. See 'help add'.")
return
fqdn = args[0].strip('.')
rtype = args[1].upper()
if not rtype in rdata_map.keys():
print('This rtype is not allowed.')
return
rdata = args[2]
try:
ttl = int(args[3])
except:
print('TTL is not numeric.')
return
zone = get_zone(fqdn, self.managed_zones)
if zone == '':
print('Zone not found for '+fqdn)
return
else:
dyn.Zone = zone
# apply change
try:
if rtype == 'MX':
dyn.CreateRecord(fqdn, rtype, rdata, ttl=ttl, preference=10)
else:
dyn.CreateRecord(fqdn, rtype, rdata, ttl=ttl)
affected_zones.add(zone)
print('{0} record for {1} added. Push changes to apply.'.format(rtype, fqdn))
except Exception as E:
print(E)
return
def do_del(self, line):
# parse args
args = parse_args(line)
if len(args) != 3:
print("Bad number of arguments. See 'help del'.")
return
fqdn = args[0].strip('.')
rtype = args[1].upper()
if not rtype in rdata_map.keys():
print('This rtype is not allowed.')
return
rdata = args[2]
zone = get_zone(fqdn, self.managed_zones)
if zone == '':
print('Zone not found for '+fqdn)
return
else:
dyn.Zone = zone
# apply change
try:
dyn.DeleteRecord(fqdn, rtype, rdata)
affected_zones.add(zone)
print('{0} record for {1} deleted. Push changes to apply.'.format(rtype, fqdn))
except Exception as E:
print(E)
return
def do_push(self, args):
if not affected_zones:
print('Nothing to push.')
return
# set of successfully pushed zones
pushed_zones = set()
# iterate through affected zones and try to push them
for zone in affected_zones:
dyn.Zone = zone
try:
dyn.Publish()
print('Zone {0} successfully pushed.'.format(zone))
pushed_zones.add(zone)
except Exception as E:
print('Failed to push zone ' + zone, end = ': ')
print(E)
# remove successfully pushed zones from affected zones
affected_zones.difference_update(pushed_zones)
def do_exit(self, args):
print('Logging out...')
try:
dyn.Logout()
except:
pass
finally:
sys.exit(0)
def do_EOF(self, line):
print('EOF')
self.do_exit('')
def help_get(self):
print('Get and print record details.')
print('Syntax: get fqdn rtype')
def help_add(self):
print("Add record. Use 0 TTL to apply default. Escape all spaces in TXT data with '\\'")
print('Syntax: add fqdn rtype rdata ttl')
def help_del(self):
print("Delete record. Escape all spaces in TXT data with '\\'")
print('Syntax: del fqdn rtype rdata')
def help_push(self):
print('Causes all pending changes to become part of the zone. Data is pushed out to the nameservers.')
def help_exit(self):
print('Cancel pending changes, disable auth token and exit.')
def default(self, line):
print('Unrecognized command: '+line)
print("Type 'help' for info.")
#################
## MAIN SCRIPT ##
#################
if __name__ == '__main__':
cli = Cli()
# authenticate to dyndns
print('Login to {0}'.format(DynectURL))
customer = input('Enter customer name ({0}): '.format(default_customer_name))
if customer == '':
customer = default_customer_name
user = input('Enter your username in DynDNS: ')
pw = getpass.getpass()
try:
dyn.Login(customer_name=customer, user_name=user, password=pw)
print('Successfully logged in.')
except Exception as E:
print(E)
sys.exit(1)
# run command line loop
while True:
try:
cli.cmdloop()
except KeyboardInterrupt:
print('^C')