-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdateDNS.py
94 lines (85 loc) · 3.06 KB
/
updateDNS.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
from typing import Tuple
import requests
import optparse
from optparse_mooi import CompactHelpFormatter
from DNSChanger import DNSChanger
from DNSRecord import DNSRecord
def get_current_ip(ip6: bool) -> str:
if not ip6:
response = requests.get("https://ipinfo.io/json", verify=True)
return response.json()["ip"]
else:
response = requests.get("https://ident.me", verify=True)
return response.text
def options() -> Tuple:
parser = optparse.OptionParser(formatter=CompactHelpFormatter())
parser.add_option(
"-k", "--key",
action="store", dest="api_key",
type=str,
help="Your Gandi API Key"
)
parser.add_option(
"-d", "--domain",
action="store", dest="domain",
type=str,
help="Your domain you want to update"
)
parser.add_option(
"-i", "--ipv6",
action="store_true", dest="ipv6", default=False,
help="Enable IPv6"
)
parser.add_option(
"-n", "--name",
action="store", dest="name",
type=str, default="@",
help="Record Name. Default is '@'"
)
parser.add_option(
"-l", "--ttl",
action="store", dest="ttl",
type=int, default=10800,
help="Record TTL. Default is 10800"
)
options, _ = parser.parse_args()
if options.api_key is None:
print("ERROR: You have to specify an API key")
exit()
if options.domain is None:
print("ERROR: You have to specify a domain")
exit()
if options.ipv6:
ip_type = "AAAA"
else:
ip_type = "A"
return options, ip_type
def update(name: str, type: str, values: str, ttl: int, api_key: str, domain: str) -> None:
rec = DNSRecord(name, type, values=[values])
dns = DNSChanger(api_key, domain, record=rec)
response = dns.get_record()
match response.status_code:
case 400:
print("Error: Invalid record type!")
case 401:
print("Bad authentication attempt because of a wrong API Key.")
case 403:
print("Access to the resource is denied. Mainly due to a lack of permissions to access it.")
case 404:
print("Record does not exist! Creating record...")
r = dns.create_record()
if r.status_code == 201:
print("The specified record has been created successfully!")
else:
print(f"Error: Could not create the specified record due to an unexpected error! Status code: {r.status_code}")
case _:
print("Updating the specified record...")
r = dns.update_record()
if r.status_code == 201:
print("The specified record has been updated successfully!")
else:
print(f"Error: Could not update the specified record due to an unexpected error! Status code: {r.status_code}")
if __name__ == "__main__":
opt, ip_type = options()
my_ip = get_current_ip(opt.ipv6)
update(name=opt.name, type=ip_type, values=my_ip, ttl=opt.ttl, api_key=opt.api_key, domain=opt.domain)