-
Notifications
You must be signed in to change notification settings - Fork 0
/
update_record_dns_ip.py
68 lines (57 loc) · 2.15 KB
/
update_record_dns_ip.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
#!/usr/bin/env python3
import os
import requests
from dotenv import load_dotenv
# Load environment variables from .env file
load_dotenv()
def get_ip() -> str:
"""Fetch external IP address."""
response = requests.get("https://api.ipify.org/")
return response.text.strip()
def has_ip_changed(current_ip: str) -> bool:
"""Check if IP has changed since last time."""
try:
with open("last_ip.txt", "r") as file:
last_ip = file.read().strip()
return last_ip != current_ip
except FileNotFoundError:
return True # File doesn't exist, assume IP has changed
def update_dns_records(ip_address: str, records: dict):
"""Update DNS records via Cloudflare API."""
zone_id = os.getenv("CLOUDFLARE_ZONE_ID")
email = os.getenv("CLOUDFLARE_EMAIL")
api_key = os.getenv("CLOUDFLARE_GLOBAL_API_KEY")
headers = {
"Content-Type": "application/json",
"X-Auth-Email": email,
"X-Auth-Key": api_key
}
endpoint_template = "https://api.cloudflare.com/client/v4/zones/{zone_id}/dns_records/{dns_record_id}"
for dns_record_id, name in records.items():
data = {
"content": ip_address,
"name": name,
"proxied": True,
"type": "A",
"ttl": 3600
}
response = requests.put(endpoint_template.format(zone_id=zone_id, dns_record_id=dns_record_id), headers=headers, json=data)
if response.status_code == 200:
print(f"Updated IP address for record {dns_record_id} ({name}) to {ip_address}")
else:
print(f"Failed to update IP address for record {dns_record_id} ({name}). Response: {response.text}")
def main():
current_ip = get_ip()
if has_ip_changed(current_ip):
# Dictionary of DNS record IDs and their associated names
records = {
"id 1": "domain name 1",
"id 2": "domain name 2"
}
update_dns_records(current_ip, records)
with open("last_ip.txt", "w") as file:
file.write(current_ip)
else:
print("IP address hasn't changed since last check. Exiting...")
if __name__ == "__main__":
main()