-
Notifications
You must be signed in to change notification settings - Fork 0
/
gdyDnsUpdate.py
69 lines (56 loc) · 1.82 KB
/
gdyDnsUpdate.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
#
# This Python script will update a DNS record at GoDaddy with your external IP address.
# Change it to reflect your needs.
#
# Created by glzbcrt ([email protected]) @ 08/05/2017.
#
import requests
import json
import time
import sys
#
# Put here your key and secret to access GoDaddy APIs.
# Get them at https://developer.godaddy.com/keys/
#
GODADDY_KEY = "MY_KEY"
GODADDY_SECRET = "MY_KEY_SECRET"
#
# Configure here the DNS zone and resource record you want to update with your external IP address.
#
GODADDY_ZONE = "myzone.com"
GODADDY_HOST = "home"
def getExternalAddress():
return requests.get("https://api.ipify.org?format=json").json()["ip"]
def updateDnsRecord(zone, record, type, value):
headers = {
"Authorization": "sso-key " + GODADDY_KEY + ":" + GODADDY_SECRET,
"Content-Type": "application/json"
}
#
# Docs for the APIs can be found at https://developer.godaddy.com/doc
#
url = "https://api.godaddy.com/v1/domains/%s/records/%s/%s" % (zone, type, record)
body = [ { "data": value } ]
rc = requests.put(url, json.dumps(body), headers = headers).status_code
if (rc == 200):
print("DNS record updated successfully.")
else:
print("Error %s while updating DNS record.")
previousAddress = ""
while 1 == 1:
try:
currentAddress = getExternalAddress()
if (currentAddress != previousAddress):
updateDnsRecord(
GODADDY_ZONE,
GODADDY_HOST,
"A",
currentAddress
)
previousAddress = currentAddress
else:
print("Address did not change.")
except:
print(sys.exc_info()[2])
# Iterate forever sleeping half hour between each one.
time.sleep(1800)