Skip to content

Instantly share code, notes, and snippets.

@dextervip
Forked from m3nu/cloudflare-update.py
Last active December 30, 2022 23:10
Show Gist options
  • Select an option

  • Save dextervip/1b90a447288a428a64f91b7dd6dbbf77 to your computer and use it in GitHub Desktop.

Select an option

Save dextervip/1b90a447288a428a64f91b7dd6dbbf77 to your computer and use it in GitHub Desktop.

Revisions

  1. dextervip revised this gist Dec 30, 2022. 1 changed file with 5 additions and 3 deletions.
    8 changes: 5 additions & 3 deletions cloudflare-update.py
    Original file line number Diff line number Diff line change
    @@ -38,12 +38,14 @@
    RECORD_ID = res['id']

    # Update or add Zone Record.
    JSON_CONTENT = json.dumps({'type': 'A', 'name': RECORD, 'content': CONTENT, 'proxied': 'false'})
    JSON_CONTENT = json.dumps({'type': 'A', 'name': RECORD, 'content': CONTENT, 'proxied': False })
    if RECORD_ID:
    print(f'Updating record {RECORD_ID}...')
    url_path = '/zones/{}/dns_records/{}'.format(ZONE_ID, RECORD_ID)
    url = base_url.format(url_path)
    requests.put(url, headers=headers, data=JSON_CONTENT)
    print(requests.put(url, headers=headers, data=JSON_CONTENT))
    else:
    print('Creating record...')
    url_path = '/zones/{}/dns_records'.format(ZONE_ID)
    url = base_url.format(url_path)
    requests.post(url, headers=headers, data=JSON_CONTENT)
    print(requests.post(url, headers=headers, data=JSON_CONTENT))
  2. dextervip revised this gist Dec 30, 2022. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion cloudflare-update.py
    Original file line number Diff line number Diff line change
    @@ -38,7 +38,7 @@
    RECORD_ID = res['id']

    # Update or add Zone Record.
    JSON_CONTENT = json.dumps({'type': 'A', 'name': RECORD, 'content': CONTENT})
    JSON_CONTENT = json.dumps({'type': 'A', 'name': RECORD, 'content': CONTENT, 'proxied': 'false'})
    if RECORD_ID:
    url_path = '/zones/{}/dns_records/{}'.format(ZONE_ID, RECORD_ID)
    url = base_url.format(url_path)
  3. dextervip revised this gist Dec 30, 2022. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion cloudflare-update.py
    Original file line number Diff line number Diff line change
    @@ -42,7 +42,7 @@
    if RECORD_ID:
    url_path = '/zones/{}/dns_records/{}'.format(ZONE_ID, RECORD_ID)
    url = base_url.format(url_path)
    requests.put(url, headers=headers data=JSON_CONTENT)
    requests.put(url, headers=headers, data=JSON_CONTENT)
    else:
    url_path = '/zones/{}/dns_records'.format(ZONE_ID)
    url = base_url.format(url_path)
  4. dextervip revised this gist Dec 30, 2022. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions cloudflare-update.py
    Original file line number Diff line number Diff line change
    @@ -14,9 +14,9 @@
    import requests
    import os

    EMAIL = os.environ['EMAIL']
    KEY = os.environ['KEY']
    ZONE = os.environ['ZONE']
    EMAIL = os.environ['CLOUDFLARE_EMAIL']
    KEY = os.environ['CLOUDFLARE_KEY']
    ZONE = os.environ['CLOUDFLARE_ZONE']
    RECORD = sys.argv[1]
    CONTENT = requests.get('http://jsonip.com').json()['ip']

  5. dextervip revised this gist Dec 30, 2022. 1 changed file with 4 additions and 3 deletions.
    7 changes: 4 additions & 3 deletions cloudflare-update.py
    Original file line number Diff line number Diff line change
    @@ -12,10 +12,11 @@
    import sys
    import json
    import requests
    import os

    EMAIL = 'XXXXX'
    KEY = 'XXXXXXX'
    ZONE = 'XXXX.com'
    EMAIL = os.environ['EMAIL']
    KEY = os.environ['KEY']
    ZONE = os.environ['ZONE']
    RECORD = sys.argv[1]
    CONTENT = requests.get('http://jsonip.com').json()['ip']

  6. Manuel Riel created this gist Jan 19, 2016.
    48 changes: 48 additions & 0 deletions cloudflare-update.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,48 @@
    #!/usr/bin/env python

    """
    Update Cloudflare zone entry to current external IP.
    Works great with servers on changing IPs or AWS spot instances.
    Usage:
    python cloudflare_update.py subdomain
    """
    import sys
    import json
    import requests

    EMAIL = 'XXXXX'
    KEY = 'XXXXXXX'
    ZONE = 'XXXX.com'
    RECORD = sys.argv[1]
    CONTENT = requests.get('http://jsonip.com').json()['ip']

    headers = {'X-Auth-Key': KEY, 'X-Auth-Email': EMAIL, 'Content-type': 'application/json'}
    base_url = 'https://api.cloudflare.com/client/v4{}'

    # Get Zone ID
    r = requests.get(base_url.format('/zones'), headers=headers)
    for res in r.json()['result']:
    if res['name'] == ZONE:
    ZONE_ID = res['id']

    # Get list of Zone DNS records
    RECORD_ID = None
    url_path = '/zones/{}/dns_records'.format(ZONE_ID)
    r = requests.get(base_url.format(url_path), headers=headers)
    for res in r.json()['result']:
    if res['name'] == '{}.{}'.format(RECORD, ZONE):
    RECORD_ID = res['id']

    # Update or add Zone Record.
    JSON_CONTENT = json.dumps({'type': 'A', 'name': RECORD, 'content': CONTENT})
    if RECORD_ID:
    url_path = '/zones/{}/dns_records/{}'.format(ZONE_ID, RECORD_ID)
    url = base_url.format(url_path)
    requests.put(url, headers=headers data=JSON_CONTENT)
    else:
    url_path = '/zones/{}/dns_records'.format(ZONE_ID)
    url = base_url.format(url_path)
    requests.post(url, headers=headers, data=JSON_CONTENT)