|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +import os |
| 4 | +import sys |
| 5 | +sys.path.insert(0, os.path.abspath('..')) |
| 6 | +import CloudFlare |
| 7 | + |
| 8 | +import re |
| 9 | +import json |
| 10 | +import requests |
| 11 | + |
| 12 | +def my_ip_address(): |
| 13 | + # This list is adjustable - plus some v6 enabled services are needed |
| 14 | + # url = 'http://myip.dnsomatic.com' |
| 15 | + # url = 'http://www.trackip.net/ip' |
| 16 | + # url = 'http://myexternalip.com/raw' |
| 17 | + url = 'https://api.ipify.org' |
| 18 | + try: |
| 19 | + ip_address = requests.get(url).text |
| 20 | + except: |
| 21 | + exit('%s: failed' % (url)) |
| 22 | + if ip_address == '': |
| 23 | + exit('%s: failed' % (url)) |
| 24 | + |
| 25 | + if ':' in ip_address: |
| 26 | + ip_address_type = 'AAAA' |
| 27 | + else: |
| 28 | + ip_address_type = 'A' |
| 29 | + |
| 30 | + return ip_address, ip_address_type |
| 31 | + |
| 32 | +def main(): |
| 33 | + try: |
| 34 | + dns_name = sys.argv[1] |
| 35 | + except: |
| 36 | + exit('usage: example-update-dynamic-dns.py fqdn-hostname') |
| 37 | + |
| 38 | + host_name, zone_name = dns_name.split('.', 1) |
| 39 | + |
| 40 | + ip_address, ip_address_type = my_ip_address() |
| 41 | + |
| 42 | + print 'MY IP: %s %s' % (dns_name, ip_address) |
| 43 | + |
| 44 | + cf = CloudFlare.CloudFlare() |
| 45 | + |
| 46 | + # grab the zone identifier |
| 47 | + try: |
| 48 | + params = {'name':zone_name} |
| 49 | + zones = cf.zones.get(params=params) |
| 50 | + except CloudFlare.CloudFlareAPIError as e: |
| 51 | + exit('/zones %d %s - api call failed' % (e, e)) |
| 52 | + except Exception as e: |
| 53 | + exit('/zones.get - %s - api call failed' % (e)) |
| 54 | + |
| 55 | + if len(zones) != 1: |
| 56 | + exit('/zones.get - %s - api call returned %d items' % (zone_name, len(zones))) |
| 57 | + |
| 58 | + zone = zones[0] |
| 59 | + |
| 60 | + zone_name = zone['name'] |
| 61 | + zone_id = zone['id'] |
| 62 | + |
| 63 | + try: |
| 64 | + params = {'name':dns_name,'match':'all','type':ip_address_type} |
| 65 | + dns_records = cf.zones.dns_records.get(zone_id, params=params) |
| 66 | + except CloudFlare.CloudFlareAPIError as e: |
| 67 | + exit('/zones/dns_records %s - %d %s - api call failed' % (dns_name, e, e)) |
| 68 | + |
| 69 | + did_update = False |
| 70 | + |
| 71 | + # update the record - unless it's already correct |
| 72 | + for dns_record in dns_records: |
| 73 | + old_ip_address = dns_record['content'] |
| 74 | + old_ip_address_type = dns_record['type'] |
| 75 | + |
| 76 | + if ip_address_type not in ['A', 'AAAA']: |
| 77 | + # we only deal with A / AAAA records |
| 78 | + continue |
| 79 | + |
| 80 | + if ip_address_type != old_ip_address_type: |
| 81 | + # only update the correct address type (A or AAAA) |
| 82 | + print 'IGNORED: %s %s ; wrong address family' % (dns_name, old_ip_address) |
| 83 | + continue |
| 84 | + |
| 85 | + if ip_address == old_ip_address: |
| 86 | + print 'UNCHANGED: %s %s' % (dns_name, ip_address) |
| 87 | + did_update = True |
| 88 | + continue |
| 89 | + |
| 90 | + dns_record_id = dns_record['id'] |
| 91 | + # update this record - we know it's the same address type |
| 92 | + dns_record = { |
| 93 | + 'name':dns_name, |
| 94 | + 'type':ip_address_type, |
| 95 | + 'content':ip_address |
| 96 | + } |
| 97 | + try: |
| 98 | + dns_record = cf.zones.dns_records.put(zone_id, dns_record_id, data=dns_record) |
| 99 | + except CloudFlare.CloudFlareAPIError as e: |
| 100 | + exit('/zones.dns_records.put %s - %d %s - api call failed' % (dns_name, e, e)) |
| 101 | + print 'UPDATED: %s %s -> %s' % (dns_name, old_ip_address, ip_address) |
| 102 | + did_update = True |
| 103 | + |
| 104 | + if did_update == False: |
| 105 | + # nothing found - so create record |
| 106 | + dns_record = { |
| 107 | + 'name':host_name, |
| 108 | + 'type':ip_address_type, |
| 109 | + 'content':ip_address |
| 110 | + } |
| 111 | + try: |
| 112 | + dns_record = cf.zones.dns_records.post(zone_id, data=dns_record) |
| 113 | + except CloudFlare.CloudFlareAPIError as e: |
| 114 | + exit('/zones.dns_records.post %s - %d %s - api call failed' % (dns_name, e, e)) |
| 115 | + print 'CREATED: %s %s' % (dns_name, ip_address) |
| 116 | + exit(0) |
| 117 | + |
| 118 | +if __name__ == '__main__': |
| 119 | + main() |
| 120 | + |
0 commit comments