-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdateip
executable file
·86 lines (77 loc) · 2.38 KB
/
updateip
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
#!/usr/bin/env python3
import requests
import json
## Update following value
cloudflare_api_key = <api_key>
domain_name = <FQDN>
user_name = <user_id>
## Follwing statement only works with following format
# wiki.k2patel.in -> which will be converted to k2patel.in
zone = domain_name.partition('.')[2]
## You mostly do not require to modify further. Unless trying to update record other than 'A'
newip = requests.get('https://ipv4.icanhazip.com')
headers = {
'X-Auth-Email': user_name,
'X-Auth-Key': cloudflare_api_key,
'Content-Type': 'application/json',
}
# Get Zone id for the domain
def get_zone(domain_zone):
z_post = {
'status': 'active',
}
j_data = json.dumps(z_post)
z_uri = 'https://api.cloudflare.com/client/v4/zones/?' + domain_zone
r = requests.get(z_uri, headers=headers, data=j_data)
j_return = json.loads(r.text)
result = j_return['result']
for value in result:
z_id = value['id']
return z_id
# Get Domain id and IP for the FQDN
def get_domain_id(domain_name, z_id):
d_post = {
'type': 'A',
'name': domain_name,
}
j_data = json.dumps(d_post)
d_uri = 'https://api.cloudflare.com/client/v4/zones/' + z_id + '/dns_records?'
r = requests.get(d_uri, headers=headers, data=j_data)
j_return = json.loads(r.text)
result = j_return['result']
for value in result:
if 'name' in value and value['name'] == domain_name:
d_ip = value['content']
d_id = value['id']
continue
return d_id, d_ip
def update_ip(new_ip, z_id, d_id, dns_name, zone):
d_post = {
'type': 'A',
'name': dns_name,
'content': new_ip,
'zone_id': z_id,
'zone_name': zone,
'data':{}
}
j_data = json.dumps(d_post)
#print(j_data)
n_uri = 'https://api.cloudflare.com/client/v4/zones/' + z_id + '/dns_records/' + d_id
#print(n_uri)
r = requests.put(n_uri, headers=headers, data=j_data)
#print(headers)
j_return = json.loads(r.text)
#print(j_return)
if j_return['success'] is True :
print('Updated : ' + dns_name)
else:
print(j_return['errors'])
if __name__ == '__main__':
z_id = get_zone(zone)
new_ip = newip.text.strip()
d_id, d_ip = get_domain_id(domain_name, z_id)
if d_ip == new_ip:
print('ip is same')
else:
print('Updating IP to : '+ new_ip)
update_ip(new_ip, z_id, d_id, domain_name, zone)