forked from tmorgans/123reg-updater
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdater.py
149 lines (120 loc) · 4.6 KB
/
updater.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
#!/usr/bin/python
import mechanize
import json
import ConfigParser
import re
from time import sleep, ctime
from urllib import urlencode, urlopen
from socket import error as SocketError
import errno
def findRowFormByAction(formlist, action):
form = 0
for f in formlist:
if f.attrs.get("action") == action:
break
form += 1
return form
def validateIpAddr(ipstring):
parts = ipstring.split('.')
if len(parts) != 4:
raise ValueError('Incorrect number of IP blocks. Must be four.')
for b in parts:
b_i = int(b)
if (b_i<0) or (b_i>255):
raise ValueError('IP Block out of range: '+b)
def printDnsEntries(entries):
print('Found {0} DNS entries'.format(len(entries)))
print('------------------------------')
print('TYPE HOST VALUE TTL')
for e in entries:
print('{0} {1} {2} {3}'.format(e['type'], e['host'], e['data'], e['record_ttl']))
def findSubdomain(subdomain, dnsrecords):
# This function searchs for a subdomain in DNS record JSON and returns
# the record.
for record in dnsrecords:
if (record["host"] == subdomain) and (record['type'] == 'A'):
return record
def getExternalIP(last):
"http://checkip.dyndns.org/"
try:
site = urlopen("http://checkip.dyndns.org/").read()
grab = re.findall('\d{2,3}.\d{2,3}.\d{2,3}.\d{2,3}', site)
address = grab[0]
return address
except SocketError as e:
if e.errno != errno.ECONNRESET:
raise
return last
def updateDnsRecords(username, password, domain, hosts, ipaddr):
# Validate IP
validateIpAddr(ipaddr)
# Create browser instance
br = mechanize.Browser()
br.set_handle_robots(False)
br.open("https://www.123-reg.co.uk/public/login")
fid = findRowFormByAction(br.forms(), "/public/login")
br.select_form(nr=fid)
br.form['username'] = username
br.form['password'] = password
br.submit()
# Here we switch to the 123-reg JSON interface
# Get list of current DNS records
response = br.open("https://www.123-reg.co.uk/secure/cpanel/manage-dns/get_dns?domain="+domain)
jsondata = json.load(response)
response.close()
# Select DNS
dnsrecords = jsondata['json']['dns']['records']
printDnsEntries(dnsrecords)
for s in hosts:
printMessage('Looking for host "'+s+'"')
record = findSubdomain(s, dnsrecords)
if record is not None:
# Valid record... Submit update
printMessage(' Updating host "'+s+'"')
record['mx_priority'] = 0
record['domain'] = domain
record['record_ttl'] = ''
record['data'] = ipaddr
request_data = urlencode(record)
response = br.open("https://www.123-reg.co.uk/secure/cpanel/manage-dns/edit_dns_record", data=request_data)
success = json.load(response)['json']['is_success']
response.close()
if success == 1:
printMessage(' Success.')
else:
printMessage(' Error updating "'+s+'"')
else:
printMessage(' Matching record for "'+s+'" not found. Skipping')
br.close()
def printMessage(message):
LOG_PATH = '/var/log/123.log'
with open(LOG_PATH, "a") as myfile:
myfile.write('[{0}] {1}\n'.format(ctime(), message))
myfile.close()
print('[{0}] {1}'.format(ctime(), message))
if __name__ == '__main__':
# Read configuration file
CONFIG_PATH = '123reg-update.conf'
config = ConfigParser.ConfigParser()
config.read(CONFIG_PATH)
username = config.get('global', 'username')
password = config.get('global', 'password')
domain = config.get('global', 'domain')
subdoms = config.get('global', 'subdomains').split(',')
print(subdoms)
interval = config.getint('global', 'interval_hours')*3600
last_ip = config.get('cache', 'lastip')
while True:
# Check lastip to current
external_ip = getExternalIP(last_ip)
if external_ip != last_ip:
printMessage('New IP address! Updating...')
updateDnsRecords(username, password, domain, subdoms, external_ip)
# Update cache
last_ip = external_ip
config.set('cache', 'lastip', external_ip)
with open(CONFIG_PATH, 'w') as h:
config.write(h)
else:
printMessage('IP not changed.')
sleep(interval)