diff --git a/README.md b/README.md index 3da312619fee755544999175d184bbef1b66d1ef..d8c46c16875b7ea47d9b7c96c772e5f3617cde71 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,6 @@ The config-template.txt file should be renamed to config.txt, and modified with Every time the script runs, it will query an external service to retrieve the external IP of the machine, compare it to the current A record in the zone at gandi.net, and then update the record if the IP has changed. Requirements: - - Python 2.7 - ipaddress module (pip install ipaddress) - requests module (pip install requests) - json module (pip install json) @@ -19,3 +18,12 @@ You can then run the script as a cron job : ``` */15 * * * * python /home/user/gandi-ddns.py ``` + +macOS + +``` +cd gandi-ddns +ln -s $(pwd) /usr/local/gandi-ddns +sudo cp gandi.ddns.plist /Library/LaunchDaemons/ +sudo launchctl /Library/LaunchDaemons/gandi.ddns.plist +``` diff --git a/gandi-ddns.py b/gandi-ddns.py index 6bee5045e9dff056bd1e51b85fb585d18aed5074..e71ae3b542307db9aa7aadc8651c6a71dc897cd0 100755 --- a/gandi-ddns.py +++ b/gandi-ddns.py @@ -1,9 +1,10 @@ -import ConfigParser as configparser +import configparser as configparser import sys import os import requests import json import ipaddress +from datetime import datetime config_file = "config.txt" @@ -17,13 +18,13 @@ def get_ip(): 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) + 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 + if not(ipaddress.IPv4Address(ip)): # check if valid IPv4 address sys.exit(2) return ip @@ -45,8 +46,8 @@ 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) + print(('Record update failed with status code: %d' % r.status_code)) + print((r.text)) sys.exit(2) print ('Zone record updated.') @@ -62,7 +63,8 @@ def main(): 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') @@ -74,7 +76,7 @@ def main(): print(url) #Discover External IP external_ip = get_ip() - print('External IP is: %s' % external_ip) + print(('External IP is: %s' % external_ip)) #Prepare record payload = {'rrset_ttl': config.get(section, 'ttl'), 'rrset_values': [external_ip]} @@ -83,10 +85,10 @@ def main(): record = get_record(url, headers) if record.status_code == 200: - print('Current record value is: %s' % json.loads(record.text)['rrset_values'][0]) + 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.') - sys.exit() + continue else: print('No existing record. Adding...') diff --git a/gandi.ddns.plist b/gandi.ddns.plist new file mode 100644 index 0000000000000000000000000000000000000000..e0c6695238c17553d5fb8c288628e39e476abc81 --- /dev/null +++ b/gandi.ddns.plist @@ -0,0 +1,30 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> +<plist version="1.0"> +<dict> + <key>Label</key> + <string>gandi.ddns</string> + <key>ProgramArguments</key> + <array> + <string>/usr/local/bin/python3</string> + <string>gandi-ddns.py</string> + </array> + + <key>RunAtLoad</key> + <true/> + + <key>StartInterval</key> + <integer>300</integer> <!-- every 5 mins --> + + <key>KeepAlive</key> + <false/> + + <key>WorkingDirectory</key> + <string>/usr/local/gandi-ddns/</string> + + <key>StandardErrorPath</key> + <string>/usr/local/var/log/gandi-ddns/output.log</string> + <key>StandardOutPath</key> + <string>/usr/local/var/log/gandi-ddns/output.log</string> +</dict> +</plist>