-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathdyndns
More file actions
43 lines (38 loc) · 1.34 KB
/
dyndns
File metadata and controls
43 lines (38 loc) · 1.34 KB
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
#!/usr/bin/python3
import cgitb
import cgi
import zlib
import sh
import os
import tempfile
cgitb.enable()
print("Content-type: text/plain\n\n")
params = cgi.parse()
name = params.get('name')
key = params.get('key')
ipi = params.get('ip')
if ipi:
ip = ipi[0]
else:
ip = os.environ.get("REMOTE_ADDR", "")
if name and key:
name_upper = name[0].upper().encode('utf-8') # Encode to bytes for zlib.adler32
if str(zlib.adler32(name_upper, 1234)) == key[0]:
with tempfile.NamedTemporaryFile(mode='w+b', delete=True) as temp_file:
file_name = temp_file.name
temp_file.write("server mydomain.com\n".encode("utf-8"))
temp_file.write("zone mydomain.com\n".encode("utf-8"))
temp_file.write(f"update delete {name[0]}.mydomain.com. A\n".encode("utf-8"))
temp_file.write(f"update add {name[0]}.mydomain.com. 3600 A {ip}\n".encode("utf-8"))
temp_file.write("show\n".encode("utf-8"))
temp_file.write("send\n".encode("utf-8"))
run = sh.nsupdate(
"-k", "/etc/dyndns/Kdynamic.mydomain.com.+015+28416.key",
"-v", file_name
)
if run.exit_code == 0:
print(f"Updated {name[0]}.mydomain.com to {ip}")
else:
print("nsupdate failed")
else:
print("Invalid key")