-
Notifications
You must be signed in to change notification settings - Fork 0
/
update-gandi-dns-record
executable file
·104 lines (83 loc) · 2.95 KB
/
update-gandi-dns-record
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
#!/usr/bin/env python3
# Author: Jan Larres <[email protected]>
# License: MIT/X11
import argparse
import fileinput
import json
import os
import requests
import signal
import sys
try:
import keyring
except ImportError:
pass
import logging
logging.basicConfig(format='%(levelname)s: %(message)s', level=logging.INFO)
log = logging.getLogger(__name__)
BASE_URL = 'https://dns.api.gandi.net/api/v5'
try:
import better_exceptions
except Exception:
pass
def sigint_handler(signal, frame):
sys.exit(1)
def parse_args():
parser = argparse.ArgumentParser(description="Update Gandi DNS record")
parser.add_argument("-v", "--verbose", action="store_true", default=False,
help="increase output verbosity")
parser.add_argument("domain", help="the domain name of the zone to change")
parser.add_argument("host", help="the host name of the record to change")
return parser.parse_args()
def main(args):
if args.verbose:
log.setLevel(logging.DEBUG)
r = requests.get('http://v4.ipv6-test.com/api/myip.php')
r.raise_for_status()
ipaddr = r.text.strip()
log.debug('IP address: ' + ipaddr)
apikey = None
keyfile = os.path.join(os.environ['HOME'], '.gandi-api-key')
if os.path.exists(keyfile):
with open(keyfile) as f:
apikey = f.readline().strip()
else:
apikey = keyring.get_password('gandi.net', 'production-api-key')
if apikey is None:
print('Unable to find API key')
sys.exit(1)
headers = {
'Content-Type': 'application/json',
'X-Api-Key': apikey
}
r = requests.get(BASE_URL + '/domains/' + args.domain, headers=headers)
r.raise_for_status()
zone = r.json()['zone_uuid']
log.debug('Zone: ' + zone)
# r = requests.get(BASE_URL + '/zones/' + zone + '/records', headers=headers).json()
r = requests.get(BASE_URL + '/zones/' + zone + '/records/' + args.host + '/A',
headers=headers)
r.raise_for_status()
old_ipaddr = r.json()['rrset_values'][0]
log.debug('Old IP address: ' + old_ipaddr)
if ipaddr == old_ipaddr:
log.debug('IP address is identical to old address; not updating DNS record')
sys.exit(0)
record_a = {
'rrset_name': args.host,
'rrset_type': 'A',
'rrset_ttl': 3600,
'rrset_values': [ipaddr]
}
r = requests.delete(BASE_URL + '/zones/' + zone + '/records/' + args.host + '/A',
headers=headers)
# r.raise_for_status()
# log.debug('Delete response: ' + str(r.json()))
r = requests.post(BASE_URL + '/zones/' + zone + '/records',
data=json.dumps(record_a), headers=headers)
log.debug('Post response A: ' + str(r.json()))
r.raise_for_status()
print('Successfully updated ' + args.host + '.' + args.domain + ' DNS record to ' + ipaddr)
if __name__ == "__main__":
signal.signal(signal.SIGINT, sigint_handler)
sys.exit(main(parse_args()))