import requests import json import os token = os.environ['cloudflare_api_token'] zoneId = 'fb89a4a1259891a31e832a1ed0f0654e' url = f'https://api.cloudflare.com/client/v4/zones/{zoneId}/dns_records' def get_public_ip(): endpoint = 'https://ipinfo.io/json' response = requests.get(endpoint, verify = True) if response.status_code != 200: exit(1) data = response.json() return data['ip'] ip = get_public_ip() # Tuples of record name and record ids records = [ ('www.resisttechmonopolies.online', '955b7c70317584ca5e0b7a9cb940b30d'), ('resisttechmonopolies.online', '8256721d2660e1585244ffdce95b148e'), ('*.resisttechmonopolies.online', 'c0bf845471c9c39a2900a6e76d92afc8') ] headers = { 'Content-Type': 'application/json', 'Authorization': f'Bearer {token}' } for record in records: data = { 'comment': 'Redirect to Sootie', 'content': ip, 'name': record[0], 'proxied': False, 'ttl': 1, 'type': 'A' } res = requests.put(f'{url}/{record[1]}', json=data, headers=headers) if res.status_code == 200: print(f'Successfully updated {record[0]} to {ip}') else: print(f'Failed to update {record[0]} with code {res.status_code}') print(f'Response Body: {res.content.decode()}')