-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmikrotik.py
executable file
·181 lines (151 loc) · 4.95 KB
/
mikrotik.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
#!/usr/bin/env python
from flask import Flask
import socket, commands
#
# Configuration
#
#
# dig binary path
dig = "/usr/bin/dig"
# nsupdate binary path
nsupdate = "/usr/bin/nsupdate"
# nsupdate key name
keyname = "nsupdate"
# nsupdate key secret
keyval = "tBoK9ec5CGBFE8VAwUKwqyKTaRidq6a1pt22r9+uFYPbLHEIpNaV8ekRm92nvooE1m3ShH60Musv2O+DGlH9Xw=="
# dns server for dig queries
dnsserver = "<DNS SERVER IP ADDRESS>"
# managed DHCP DNS zone
zone = "<DNS ZONE. EXAMPLE.COM>"
# DNS TTL (Default: 600 sec)
ttl = "600"
#
#
# Configuration end
#
# DO NOT CHANGE
#
#
def is_valid_ipv4_address(address):
try:
socket.inet_pton(socket.AF_INET, address)
except AttributeError: # no inet_pton here, sorry
try:
socket.inet_aton(address)
except socket.error:
return False
return address.count('.') == 3
except socket.error: # not a valid address
return False
return True
def is_valid_ipv6_address(address):
try:
socket.inet_pton(socket.AF_INET6, address)
except socket.error: # not a valid address
return False
return True
def is_valid_address(address):
if (is_valid_ipv4_address(address) or is_valid_ipv6_address(address)):
return True
else:
return False
def get_ip_type(ip):
if is_valid_ipv4_address(ip):
return 'A'
else:
return 'AAAA'
def get_full_ptr(ip):
i = ip.split('.')
#return i
return "{i4}.{i3}.{i2}.{i1}.in-addr.arpa".format(i4=i[3], i3=i[2], i2=i[1], i1=i[0])
def is_valid_hostname(host = None):
chars = "ABCDEFGHIJKLMNOUPQRSTXYVWZabcdefghijklmnoupqrstxyvwz0987654321-"
valid = all(c in chars for c in host)
return valid
def host_in_dns(host):
h = "{host}.{zone}".format(host=host,zone=zone)
cmd = "{dig} +short {host} @{dnsserver}".format(dig=dig,host=h,dnsserver=dnsserver)
retval,output = commands.getstatusoutput(cmd)
if output == "":
return False
else:
return True
def get_ip(host):
h = "{host}.{zone}".format(host=host,zone=zone)
cmd = "{dig} +short {host} @{dnsserver}".format(dig=dig,host=h,dnsserver=dnsserver)
retval,output = commands.getstatusoutput(cmd)
return output
def rev_in_dns(ip):
cmd = "{dig} +short -x {ip} @{dnsserver}".format(dig=dig,ip=ip,dnsserver=dnsserver)
retval,output = commands.getstatusoutput(cmd)
if output == "":
return False
else:
return True
app = Flask(__name__)
@app.route('/')
def home():
return 'Mikrotik DHCP DNS Updater'
@app.route('/update/<host>/<ip>')
def update(host=None,ip=None):
if host == None:
return 'Something got wrong'
else:
if is_valid_address(ip):
if is_valid_hostname(host):
if host_in_dns(host): # we need to delete old entries first
delete(host)
fqdn = "{host}.{zone}".format(host=host,zone=zone)
t = get_ip_type(ip)
ptr = get_full_ptr(ip)
batch = [
'update add {fqdn} {ttl} {type} {ip}'.format(fqdn=fqdn, ttl=ttl, type=t, ip=ip),
'',
'update add {ptr} {ttl} PTR {fqdn}.'.format(ptr=ptr, ttl=ttl, fqdn=fqdn),
'send',
'quit\n'
]
cmd = 'echo "{batch}" | {nsupdate} -y {keyname}:{keyval}'.format(batch='\n'.join(batch), nsupdate=nsupdate, keyname=keyname, keyval=keyval)
#return ' '.join(batch)
retval,output = commands.getstatusoutput(cmd)
if retval == 0:
return "OK"
else:
return output
else:
return "Invalid hostname"
else:
return "Wrong IP"
@app.route('/delete/<host>')
def delete(host=None):
if is_valid_hostname(host):
if host_in_dns(host):
ip = get_ip(host)
t = get_ip_type(ip)
fqdn = "{host}.{zone}".format(host=host,zone=zone)
if rev_in_dns(ip):
ptr = get_full_ptr(ip)
batch = [
'update delete {ptr} PTR'.format(ptr=ptr),
'', #DNS BUG from 2004 new line needs to be added
'update delete {fqdn} {type}'.format(fqdn=fqdn,type=t),
'send',
'quit\n'
]
else:
batch = [
'update delete {fqdn} {type}'.format(fqdn=fqdn,type=t),
'send',
'quit\n'
]
cmd = 'echo "{batch}" | {nsupdate} -y {keyname}:{keyval}'.format(batch='\n'.join(batch), nsupdate=nsupdate, keyname=keyname, keyval=keyval)
retval,output = commands.getstatusoutput(cmd)
if retval == 0:
return "OK"
else:
return output
else:
return "Host not in dns"
else:
return "Invalid Hostname"
app.run(host='0.0.0.0')