Private GIT

Skip to content
Snippets Groups Projects
Select Git revision
  • 70edd27f2e6cb7350f426d8c7c8dac2e83887d27
  • master default protected
  • fix_nzb_cat
  • develop
  • guessit2-minimal
  • ssl_warning
  • UHD-qualities
  • fix_providers8
  • !
  • tvvault
  • provider_alpharatio
  • v5.1.1
  • v5.1
  • v5.0.3
  • v5.0.2
  • v5.0.1
  • v5.0
  • v4.2.1.07
  • v4.2.1.06
  • v4.2.1.05
  • v4.2.1.04
  • v4.2.1.03
  • v4.2.1.02
  • v4.2.1.01
  • v4.2.1.0
  • v4.2.0.6
  • v4.2.0.5
  • v4.2.0.4
  • v4.2.0.3
  • v4.2.0.2
  • v4.2.0.1
31 results

history.py

Blame
  • To find the state of this project's repository at the time of any of these versions, check out the tags.
    gandi-ddns.py 2.75 KiB
    import configparser as configparser
    import sys
    import os
    import requests
    import json
    import ipaddress
    from datetime import datetime
    
    config_file = "config.txt"
    
    SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__))
    
    def get_ip():
            #Get external IP
            try:
              # Could be any service that just gives us a simple raw ASCII IP address (not HTML etc)
              r = requests.get('https://api.ipify.org', timeout=3)
            except Exception:
              print('Failed to retrieve external IP.')
              sys.exit(2)
            if r.status_code != 200:
              print(('Failed to retrieve external IP. Server responded with status_code: %d' % r.status_code))
              sys.exit(2)
    
            ip = r.text.rstrip() # strip \n and any trailing whitespace
    
            if not(ipaddress.IPv4Address(ip)): # check if valid IPv4 address
              sys.exit(2)
    
            return ip
    
    def read_config(config_path):
            #Read configuration file
            cfg = configparser.ConfigParser()
            cfg.read(config_path)
    
            return cfg
    
    def get_record(url, headers):
            #Get existing record
            r = requests.get(url, headers=headers)
    
            return r
    
    def update_record(url, headers, payload):
            #Add record
            r = requests.put(url, headers=headers, json=payload)
            if r.status_code != 201:
              print(('Record update failed with status code: %d' % r.status_code))
              print((r.text))
              sys.exit(2)
            print ('Zone record updated.')
    
            return r
    
    
    def main():
      path = config_file
      if not path.startswith('/'):
        path = os.path.join(SCRIPT_DIR, path)
      config = read_config(path)
      if not config:
        sys.exit("Please fill in the 'config.txt' file.")
    
      for section in config.sections():
        print('%s - section %s' % (str(datetime.now()), section))
    
        #Retrieve API key
        apikey = config.get(section, 'apikey')
    
        #Set headers
        headers = { 'Content-Type': 'application/json', 'X-Api-Key': '%s' % config.get(section, 'apikey')}
    
        #Set URL
        url = '%sdomains/%s/records/%s/A' % (config.get(section, 'api'), config.get(section, 'domain'), config.get(section, 'a_name'))
        print(url)
        #Discover External IP
        external_ip = get_ip()
        print(('External IP is: %s' % external_ip))
    
        #Prepare record
        payload = {'rrset_ttl': config.get(section, 'ttl'), 'rrset_values': [external_ip]}
    
        #Check current record
        record = get_record(url, headers)
    
        if record.status_code == 200:
          print(('Current record value is: %s' % json.loads(record.text)['rrset_values'][0]))
          if(json.loads(record.text)['rrset_values'][0] == external_ip):
            print('No change in IP address. Goodbye.')
            continue
        else:
          print('No existing record. Adding...')
    
        update_record(url, headers, payload)
    
    if __name__ == "__main__":
      main()