-
Notifications
You must be signed in to change notification settings - Fork 0
/
updateCloud.py
49 lines (40 loc) · 1.36 KB
/
updateCloud.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
#!/usr/bin/python3
import requests
import json
import sys
IP_API = 'https://api.ipify.org?format=json'
# Get CF API Key: https://support.cloudflare.com/hc/en-us/articles/200167836-Where-do-I-find-my-Cloudflare-API-key-
CF_API_KEY = 'REPLACE ME'
# Your cloudflare email address
CF_EMAIL = '[email protected]'
# Your zone id is located on the main cloudflare domain dashboard
ZONE_ID = 'REPLACE ME'
# Run script once without this set and it'll retrive a list of records for you to find the ID to update here
RECORD_ID = ''
if not RECORD_ID:
resp = requests.get(
'https://api.cloudflare.com/client/v4/zones/{}/dns_records'.format(ZONE_ID),
headers={
'X-Auth-Key': CF_API_KEY,
'X-Auth-Email': CF_EMAIL
})
print(json.dumps(resp.json(), indent=4, sort_keys=True))
print('Please find the DNS record ID you would like to update and entry the value into the script')
sys.exit(0)
resp = requests.get(IP_API)
ip = resp.json()['ip']
resp = requests.put(
'https://api.cloudflare.com/client/v4/zones/{}/dns_records/{}'.format(
ZONE_ID, RECORD_ID),
json={
'type': 'A',
'name': 'test',
'content': ip,
'proxied': False
},
headers={
'X-Auth-Key': CF_API_KEY,
'X-Auth-Email': CF_EMAIL
})
assert resp.status_code == 200
print('updated dns record for {}'.format(ip))