-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdns.py
68 lines (58 loc) · 2.3 KB
/
dns.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
##########################################################################
################################## PyVOLD ################################
########################### osama@ipdevops.com ###########################
##########################################################################
import requests
import json, os
from requests.auth import HTTPBasicAuth #don't remove
api_key=""
api_secret=""
domains = ['domain1.com', 'domain2.com', 'domain3.com']
record_name = "@"
here = os.path.dirname(__file__)
def check_ip_address():
# Option#1 CHECK Current IP address for the DNS RECORD FROM GODADDY
'''
DNS_A = requests.get(url, headers=headers)
current_ip=DNS_A.json()[0]['data']
'''
# Option#2 Check DNS record from a file - to avoid many API requestes
current_ip = open(os.path.join(here, '.newip'), 'r').readline()
# Get the servers public IP address
ipresponse = requests.get("https://ipdevops.com/api/myip").json()
public_ip = ipresponse['ip']
return public_ip, current_ip
def compare_ips(public_ip, current_ip):
if current_ip == public_ip:
print("IP was not changed")
data = []
else:
data = [
{
"data": public_ip,
"name": record_name,
"ttl": 600,
"type": "A"
}
]
return data
def update_dns_record(url, public_ip, current_ip, data):
headers = {"Authorization" : "sso-key {}:{}".format(api_key, api_secret), 'Content-type':'application/json', 'Accept':'application/json'}
post_address = requests.patch(url, data=json.dumps(data), headers=headers)
if post_address.status_code == 200:
print("IP was changed from {} to {} for {}".format(current_ip, public_ip, url))
# Write the update in the hidden file
file = open(os.path.join(here, '.newip'),'w')
file.write(public_ip)
file.close()
else:
print('Error Occured')
print(post_address.status_code)
def main():
public_ip, current_ip = check_ip_address()
data = compare_ips(public_ip, current_ip)
if len(data) != 0:
for domain in domains:
url="https://api.godaddy.com/v1/domains/{}/records".format(domain)
update_dns_record(url, public_ip, current_ip, data)
main()