diff --git a/cloudflare-dns-new.py b/cloudflare-dns-new.py deleted file mode 100644 index 46da1cc..0000000 --- a/cloudflare-dns-new.py +++ /dev/null @@ -1,97 +0,0 @@ -import sys -import ipaddress -from configparser import ConfigParser -import json -import copy -import CloudFlare - - -class DNSUpdater: - def __init__(self, args): - # Parse config - config = ConfigParser() - config.read('config.ini') - - self.HOSTNAME = config['server']['HOSTNAME'] - __TOKEN = config['cloudflare']['TOKEN'] - - # Parse args - try: - ipversion = args[1].lstrip('-') - ipaddress_new = args[2] - except IndexError: - ipaddress_new = '' - ipversion = 'skip' - - match ipversion: - case '4': self.IPVER = 'ipv4' - case '6': self.IPVER = 'ipv6' - case 'skip': self.IPVER = ipversion - case other: raise ValueError(f"Wrong IP Address version given: {ipversion}") - - self.IPNEW = ipaddress_new - - # Cloudflare class - self._cloudflare = CloudFlare.CloudFlare(token=__TOKEN) - - with open('cloudflare.json', 'r') as file: - self.dns_replacements = json.load(file) - - def get_zones(self): - # Cloudflare get zones from API token - zones = self._cloudflare.zones.get() - for zone in zones: - self.zone_id = zone['id'] - self.zone_name = zone['name'] - print(f"Zone ID: {zone['id']} | Zone Name: {zone['name']}") - - def get_dns_records(self, log=False): - dns_records_all = self._cloudflare.zones.dns_records.get(self.zone_id) - - dns_records_to_update = {'A' : [], 'AAAA' : []} - for dnsrecord in dns_records_all: - if dnsrecord['type'] in ['A', 'AAAA']: - if log: - print(f"DNS Name: {dnsrecord['name']} | Type : {dnsrecord['type']} | IP: {dnsrecord['content']}") - dns_records_to_update[dnsrecord['type']].append(dnsrecord) - else: - if log: - print(f"| Unknow DNS | Type: {dnsrecord['type']} | Zone: {dnsrecord['name']}") - - self.DNSRECORDS = copy.deepcopy(dns_records_to_update) - - def update_records(self): - if self.IPVER == 'skip': - self.get_dns_records(log=True) - return - - self.get_dns_records() - - ipinformation = { - 'ipv4': [ipaddress.IPv4Address, 'A'], - 'ipv6': [ipaddress.IPv6Address, 'AAAA'] - } - - print(f"Updating IP of version {self.IPVER}") - assert isinstance(ipaddress.ip_address(self.IPNEW), ipinformation[self.IPVER][0]), f'IP {self.IPNEW} is not a valid IPv6 address' - dnstype = ipinformation[self.IPVER][1] - - names_replace = self.dns_replacements[dnstype] - print(names_replace) - for name in names_replace: - fullname = f"{name}.{self.HOSTNAME}" - - for record in self.DNSRECORDS[dnstype]: - if record['name'] == fullname: - new_dnsrecord = {"name": fullname, "type": dnstype, "content": self.IPNEW} - print(f"Sending request {new_dnsrecord}") - res = self._cloudflare.zones.dns_records.patch(self.zone_id, record['id'], data=new_dnsrecord) - if res['content'] != new_dnsrecord['content']: - print(res) - - - -if __name__ == '__main__': - dnsupdate = DNSUpdater(sys.argv) - dnsupdate.get_zones() - dnsupdate.update_records() \ No newline at end of file diff --git a/cloudflare_script.py b/cloudflare_script.py index 2b7a836..46da1cc 100644 --- a/cloudflare_script.py +++ b/cloudflare_script.py @@ -2,101 +2,96 @@ import sys import ipaddress from configparser import ConfigParser import json +import copy import CloudFlare -def main(): - config = ConfigParser() - config.read('config.ini') - try: - ipversion = sys.argv[1].lstrip('-') - ip_address_new = sys.argv[2] - except IndexError: - ip_address_new = '' - ipversion = 'skipping' +class DNSUpdater: + def __init__(self, args): + # Parse config + config = ConfigParser() + config.read('config.ini') - hostname = config['server']['HOSTNAME'] + self.HOSTNAME = config['server']['HOSTNAME'] + __TOKEN = config['cloudflare']['TOKEN'] - cf = CloudFlare.CloudFlare(token=config['cloudflare']['TOKEN']) - zones = cf.zones.get() + # Parse args + try: + ipversion = args[1].lstrip('-') + ipaddress_new = args[2] + except IndexError: + ipaddress_new = '' + ipversion = 'skip' - for zone in zones: - zone_id = zone['id'] - zone_name = zone['name'] - print("zone_id=%s zone_name=%s" % (zone_id, zone_name)) - - dns_records = cf.zones.dns_records.get(zone_id) - - dns_names = {'A' : [], 'AAAA' : []} - for dnsrecord in dns_records: - if ipversion == 'skipping': - print(f"DNS Name: {dnsrecord['name']} | ID: {dnsrecord['id']} | Type : {dnsrecord['type']} | IP: {dnsrecord['content']}") + match ipversion: + case '4': self.IPVER = 'ipv4' + case '6': self.IPVER = 'ipv6' + case 'skip': self.IPVER = ipversion + case other: raise ValueError(f"Wrong IP Address version given: {ipversion}") - 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---------------------------" - ) + self.IPNEW = ipaddress_new + # Cloudflare class + self._cloudflare = CloudFlare.CloudFlare(token=__TOKEN) - with open('cloudflare.json', 'r') as file: - values = json.load(file) - print(values) + with open('cloudflare.json', 'r') as file: + self.dns_replacements = json.load(file) + + def get_zones(self): + # Cloudflare get zones from API token + zones = self._cloudflare.zones.get() + for zone in zones: + self.zone_id = zone['id'] + self.zone_name = zone['name'] + print(f"Zone ID: {zone['id']} | Zone Name: {zone['name']}") - match ipversion: - case '4': - print("Updating IPv4") - assert isinstance(ipaddress.ip_address(ip_address_new), ipaddress.IPv4Address), f'IP {ip_address_new} is not a valid IPv4 address' - names_replace = values['A'] - print(names_replace) - for name in names_replace: - fullname = f"{name}.{hostname}" - print(fullname) + def get_dns_records(self, log=False): + dns_records_all = self._cloudflare.zones.dns_records.get(self.zone_id) - for x in dns_records: - if x['name'] == fullname: - dns_record_id = x['id'] - print(dns_record_id) + dns_records_to_update = {'A' : [], 'AAAA' : []} + for dnsrecord in dns_records_all: + if dnsrecord['type'] in ['A', 'AAAA']: + if log: + print(f"DNS Name: {dnsrecord['name']} | Type : {dnsrecord['type']} | IP: {dnsrecord['content']}") + dns_records_to_update[dnsrecord['type']].append(dnsrecord) + else: + if log: + print(f"| Unknow DNS | Type: {dnsrecord['type']} | Zone: {dnsrecord['name']}") - if fullname in dns_names['A']: - new_dnsrecords = {"name": fullname, "type": "A", "content": ip_address_new} - print(f"Sending request {new_dnsrecords}") - try: - r = cf.zones.dns_records.patch(zone_id, dns_record_id, data=new_dnsrecords) - print(r) - except: - print("Error pushing entry") + self.DNSRECORDS = copy.deepcopy(dns_records_to_update) + + def update_records(self): + if self.IPVER == 'skip': + self.get_dns_records(log=True) + return - case '6': - print("Updating IPv6") - assert isinstance(ipaddress.ip_address(ip_address_new), ipaddress.IPv6Address), f'IP {ip_address_new} is not a valid IPv6 address' - names_replace = values['AAAA'] - print(names_replace) - for name in names_replace: - fullname = f"{name}.{hostname}" - print(fullname) + self.get_dns_records() - for x in dns_records: - if x['name'] == fullname: - dns_record_id = x['id'] - print(dns_record_id) + ipinformation = { + 'ipv4': [ipaddress.IPv4Address, 'A'], + 'ipv6': [ipaddress.IPv6Address, 'AAAA'] + } - if fullname in dns_names['AAAA']: - new_dnsrecords = {"name": fullname, "type": "AAAA", "content": ip_address_new} - print(f"Sending request {new_dnsrecords}") - 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: - print(f"Error with Ip version passed, {ipversion} is invalid") + print(f"Updating IP of version {self.IPVER}") + assert isinstance(ipaddress.ip_address(self.IPNEW), ipinformation[self.IPVER][0]), f'IP {self.IPNEW} is not a valid IPv6 address' + dnstype = ipinformation[self.IPVER][1] + + names_replace = self.dns_replacements[dnstype] + print(names_replace) + for name in names_replace: + fullname = f"{name}.{self.HOSTNAME}" + + for record in self.DNSRECORDS[dnstype]: + if record['name'] == fullname: + new_dnsrecord = {"name": fullname, "type": dnstype, "content": self.IPNEW} + print(f"Sending request {new_dnsrecord}") + res = self._cloudflare.zones.dns_records.patch(self.zone_id, record['id'], data=new_dnsrecord) + if res['content'] != new_dnsrecord['content']: + print(res) + if __name__ == '__main__': - main() \ No newline at end of file + dnsupdate = DNSUpdater(sys.argv) + dnsupdate.get_zones() + dnsupdate.update_records() \ No newline at end of file