Created
November 2, 2015 21:41
-
-
Save volnt/ed4f5d0f2f14a9f87836 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
API_KEY="<YOUR-API-KEY>" | |
BASE_URL="https://api.cloudflare.com/client/v4/" | |
EMAIL="<YOUR-EMAIL>" | |
DOMAIN="<YOUR-DOMAIN>" | |
SUBDOMAIN="<YOUR-SUBDOMAIN>" | |
def get_ip_address(): | |
r = requests.get("https://api.ipify.org?format=json") | |
return r.json()["ip"] | |
def get_zone_id(): | |
r = requests.get("{}zones?name={}".format(BASE_URL, DOMAIN), headers={ | |
"X-Auth-Email": EMAIL, | |
"X-Auth-Key": API_KEY, | |
"Content-Type": "application/json" | |
}) | |
return r.json()["result"][0]["id"] | |
def get_dns_record(zone_id): | |
r = requests.get("{}zones/{}/dns_records?name={}.{}".format(BASE_URL, zone_id, SUBDOMAIN, DOMAIN), headers={ | |
"X-Auth-Email": EMAIL, | |
"X-Auth-Key": API_KEY, | |
"Content-Type": "application/json" | |
}) | |
return r.json()["result"][0] | |
def update_dns_record(zone_id, dns_record, ip_address): | |
dns_record["content"] = ip_address | |
r = requests.put("{}zones/{}/dns_records/{}".format(BASE_URL, zone_id, dns_record["id"]), headers={ | |
"X-Auth-Email": EMAIL, | |
"X-Auth-Key": API_KEY, | |
"Content-Type": "application/json" | |
}, json=dns_record) | |
return r.json() | |
def main(): | |
zone_id = get_zone_id() | |
ip_address = get_ip_address() | |
dns_record = get_dns_record(zone_id) | |
return update_dns_record(zone_id, dns_record, ip_address) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment