Jump to content
i wrote this Cloudflare dns updater in python:
 
 
def update_subdomain(subdomain, ext_ip, proxied=True):
zone_id = get_zone_id() #correct
identifier = get_identifier(subdomain) #correct
url = f"https://api.cloudflare.com/client/v4/zones/{zone_id}/dns_records/{identifier}" #correct url

payload = {
"content": ext_ip,
"name": subdomain,
"type": "A",
"comment": "Automatically updated by Cloudflare.py",
"ttl": 1,
"proxied": proxied
}

headers = get_headers() #correct auth headers

response = requests.request("PUT", url, json=payload, headers=headers)

print(response.json()) # -> {'result': {'id': 'XXX', 'zone_id': 'XXX', 'zone_name': 'XXX', 'name': 'XXX', 'type': 'A', 'content': 'XXX', 'proxiable': True, 'proxied': False, 'ttl': 1, 'locked': False, 'meta': {'auto_added': False, 'managed_by_apps': False, 'managed_by_argo_tunnel': False, 'source': 'primary'}, 'comment': 'Automatically updated by Cloudflare.py', 'tags': [], 'created_on': '2023-10-09T08:12:39.52179Z', 'modified_on': '2023-12-09T22:46:19.848655Z'}, 'success': True, 'errors': [], 'messages': []}
 
When i call this function:
 
update_subdomain("test", 1.2.3.4, True)
 
it works completly fine and the record shows up in teh dashbord and works.
 
but when i call it again with a different subdomain but the same external ip address:

update_subdomain("test2", 1.2.3.4, True)

it removes the previous record for test.example.com and overwrites it with test2.example.com.

 

i want to update the ip address for a record if that record exists and if it dopesnt exist yet, add that record.

How do i do that?

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×