-
Notifications
You must be signed in to change notification settings - Fork 21
/
cfautoupdater.py
92 lines (67 loc) · 3.52 KB
/
cfautoupdater.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
import requests, time, json, configparser, smtplib, logging, datetime
# Reading the keys from the cfauth.ini file
config = configparser.ConfigParser()
config.read('cfauth.ini')
zone_id = config.get('tokens', 'zone_id')
bearer_token = config.get('tokens', 'bearer_token')
record_id = config.get('tokens', 'record_id')
# Setting up the logger (a file where it records all IP changes)
logging.basicConfig(level=logging.INFO, filename='ipchanges.log', format='%(levelname)s :: %(message)s')
# The headers we want to use
headers = {
"Authorization": f"Bearer {bearer_token}",
"content-type": "application/json"
}
while True:
# Getting the initial data of your A Record
a_record_url = requests.get(f"https://api.cloudflare.com/client/v4/zones/{zone_id}/dns_records/{record_id}", headers=headers)
arecordjson = a_record_url.json()
# This is the current IP that your chosen A record has been set to on Cloudflare
current_set_ip = arecordjson['result']['content']
# This gets your current live external IP (whether that is the same as the A record or not)
currentip = requests.get("https://api.ipify.org?format=json")
# Status code should be 200, otherwise the API is probably down (this can happen quite a bit)
ipcheck_status = currentip.status_code
# Handling any API errors (otherwise we'd be trying to change the IP to some random HTML)
while ipcheck_status != 200:
time.sleep(10)
currentip = requests.get("https://api.ipify.org?format=json")
ipcheck_status = currentip.status_code
currentactualip = currentip.json()['ip']
# This loop checks your live IP every 5 minutes to make sure that it's the same one as set in your DNS record
while currentactualip == current_set_ip:
time.sleep(300) # Wait for 300 seconds (5 minutes)
currentip = requests.get("https://api.ipify.org?format=json") # Then it checks if your IP is still the same or not.
ipcheck_status = currentip.status_code
# Handling any API errors AGAIN
while ipcheck_status != 200:
time.sleep(10)
currentip = requests.get("https://api.ipify.org?format=json")
ipcheck_status = currentip.status_code
currentactualip = currentip.json()['ip']
else: # If your live IP is NOT the same as the A Record's IP
pass
# The "Payload" is what we want to change in the DNS record JSON (in this case, it's our IP)
payload = {"content": currentactualip}
# Change the IP using a PATCH request
requests.patch(f"https://api.cloudflare.com/client/v4/zones/{zone_id}/dns_records/{record_id}", headers=headers, data=json.dumps(payload))
# Get the time of the IP change
now = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
# LOG THE CHANGE
logging.info(f"{now} - IP change from {current_set_ip} to {currentactualip}")
# Sends an email to you to let you know everything has been updated.
# If you don't want this, just delete everything below this comment.
sender = 'sender@google.com'
receivers = ['receiver@example.com']
message = f"""From: Server <sender@google.com>
To: Your email <receiver@example.com>
Subject: DNS IP Updated
The server's IP has changed from {current_set_ip} to {currentactualip}.
The DNS records have been updated.
"""
smtpObj = smtplib.SMTP('smtp.example.com', port=587)
smtpObj.connect("smtp.example.com", port=587)
smtpObj.ehlo()
smtpObj.starttls()
smtpObj.login("username", "password")
smtpObj.sendmail(sender, receivers, message)