Last active
October 4, 2022 09:33
-
-
Save radupotop/e65bc1a16cac0d1fd00e2ec004b03e7d to your computer and use it in GitHub Desktop.
This file contains 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
#!/usr/bin/env python3 | |
import CloudFlare | |
import argparse | |
import sys | |
if __name__ == "__main__": | |
parser = argparse.ArgumentParser() | |
parser.add_argument("-e", "--email", required=True, help="The Cloudflare login email to use") | |
parser.add_argument("-n", "--hostname", required=True, help="The hostname to update, e.g. mydyndns.mydomain.com") | |
parser.add_argument("-k", "--api-key", required=True, help="The Cloudflare global API key to use. NOTE: Domain-specific API tokens will NOT work!") | |
parser.add_argument("-i", "--ip-address", required=True, help="Which IP address to update the record to") | |
parser.add_argument("-t", "--ttl", default=60, type=int, help="The TTL of the records in seconds (or 1 for auto)") | |
args = parser.parse_args() | |
# Initialize Cloudflare API client | |
cf = CloudFlare.CloudFlare( | |
email=args.email, | |
token=args.api_key | |
) | |
# Get zone ID (for the domain). This is why we need the API key and the domain API token won't be sufficient | |
zone = ".".join(args.hostname.split(".")[-2:]) # domain = test.mydomain.com => zone = mydomain.com | |
zones = cf.zones.get(params={"name": zone}) | |
if len(zones) == 0: | |
print(f"Could not find CloudFlare zone {zone}, please check domain {args.hostname}") | |
sys.exit(2) | |
zone_id = zones[0]["id"] | |
# Fetch existing A record | |
a_record = cf.zones.dns_records.get(zone_id, params={"name": args.hostname, "type": "A"})[0] | |
# Update record & save to cloudflare | |
a_record["ttl"] = args.ttl # 1 == auto | |
a_record["content"] = args.ip_address | |
cf.zones.dns_records.put(zone_id, a_record["id"], data=a_record) | |
# ./update-dns.py --api-key ... --email cloudflare@mydomain.com --ttl 300 --ip 1.2.3.4 --hostname mysubdomain.domain.com |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment