-
Notifications
You must be signed in to change notification settings - Fork 1
/
gddu.py
executable file
·58 lines (46 loc) · 1.78 KB
/
gddu.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
#!/usr/bin/env python3
import argparse
import configparser
import json
import os
import sys
import urllib.request
class GoDaddyDNSUpdater:
def __init__(self, mode='production'):
config = configparser.ConfigParser()
config.read(['gddu.conf', os.path.expanduser('~/.gddu.conf'),'/etc/gddu.conf'])
self.key = config.get(mode, 'api_key')
self.secret = config.get(mode, 'api_secret')
self.domain = config.get(mode, 'domain')
self.hostname = config.get(mode, 'hostname')
self.base_url = config.get(mode, 'api_url')
self.real_ip = self.get_real_ip()
def get_real_ip(self):
url = 'http://whatismyip.akamai.com/'
req = urllib.request.Request(url)
res = urllib.request.urlopen(req).read().decode('UTF-8')
return res
def update_a_record(self):
url = '{}/v1/domains/{}/records/A/{}'.format(self.base_url, self.domain, self.hostname)
headers = {
'Accept': 'application/json',
'Authorization': 'sso-key {}:{}'.format(self.key, self.secret),
'Content-Type': 'application/json',
}
data = json.dumps([{
"name": self.hostname,
"ttl": 600,
"type": "A",
"data": self.real_ip,
}]).encode('UTF-8')
req = urllib.request.Request(url, data, headers, None, False, 'PUT')
res = urllib.request.urlopen(req)
res.read()
def main():
parser = argparse.ArgumentParser()
parser.add_argument('--test', action='store_true', help='Run in "test" environment (by default, it runs in "production"')
args = parser.parse_args()
mode = 'test' if args.test else 'production'
GoDaddyDNSUpdater(mode).update_a_record()
if __name__ == "__main__":
main()