diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8f38060 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ + +venv/ diff --git a/cloudflare_script.py b/cloudflare_script.py index 83a2d9f..2b7a836 100644 --- a/cloudflare_script.py +++ b/cloudflare_script.py @@ -31,7 +31,16 @@ def main(): for dnsrecord in dns_records: if ipversion == 'skipping': print(f"DNS Name: {dnsrecord['name']} | ID: {dnsrecord['id']} | Type : {dnsrecord['type']} | IP: {dnsrecord['content']}") - dns_names[dnsrecord['type']].append(dnsrecord["name"]) + + if dnsrecord['type'] in ['A', 'AAAA']: + dns_names[dnsrecord['type']].append(dnsrecord["name"]) + else: + print(f"Cannot resolve record {dnsrecord['name']} because of type {dnsrecord['type']}") + print(f"------ Full DNS record ------\n" + f"{dnsrecord}" + f"\n---------------------------" + ) + with open('cloudflare.json', 'r') as file: values = json.load(file) @@ -55,8 +64,11 @@ def main(): if fullname in dns_names['A']: new_dnsrecords = {"name": fullname, "type": "A", "content": ip_address_new} print(f"Sending request {new_dnsrecords}") - r = cf.zones.dns_records.put(zone_id, dns_record_id, data=new_dnsrecords) - print(r) + try: + r = cf.zones.dns_records.patch(zone_id, dns_record_id, data=new_dnsrecords) + print(r) + except: + print("Error pushing entry") case '6': print("Updating IPv6") @@ -75,8 +87,11 @@ def main(): if fullname in dns_names['AAAA']: new_dnsrecords = {"name": fullname, "type": "AAAA", "content": ip_address_new} print(f"Sending request {new_dnsrecords}") - r = cf.zones.dns_records.put(zone_id, dns_record_id, data=new_dnsrecords) - print(r) + try: + r = cf.zones.dns_records.patch(zone_id, dns_record_id, data=new_dnsrecords) + print(r) + except: + print("Error pushing entry") case 'skipping': print("Done") case other: diff --git a/set_ip.sh b/set_ip.sh index 149b3f5..155e0a6 100644 --- a/set_ip.sh +++ b/set_ip.sh @@ -13,8 +13,9 @@ else if [ ! -e cloudflare.json ] || [ ! -e config.ini ]; then echo "Cloudflare config not found, is the script run in the correct directory?" else - ipv4=$(curl -k -s https://am.i.mullvad.net/ip) + ipv4=$(curl -k -s https://am.i.mullvad.net/ip -4) ipv6=$(curl -k -s https://ipv6.am.i.mullvad.net/) + source venv/bin/activate python3 ./cloudflare_script.py 4 "$ipv4" python3 ./cloudflare_script.py 6 "$ipv6" fi