diff --git a/README.md b/README.md index d971c40778824524f8b464b0701dc214c7966322..af3ad5825b299e833bfc62fae8a69a2186d485b4 100644 --- a/README.md +++ b/README.md @@ -5,3 +5,7 @@ http://doc.livedns.gandi.net/ The config-template.txt file should be renamed to config.txt, and modified with your gandi.net API key, domain name, and A-record (@, dev, home, pi, etc). Every time the script runs, it will query an external service to retrieve the external IP of the machine, compare it to the current record (if any) in the zone at gandi.net, and then add a new record (if none currently exists), or delete then add a new record (if a record already exists). + +Requirements: + - Python 2.7 + - ipaddress module (pip install ipaddress) diff --git a/gandi-ddns.py b/gandi-ddns.py index a1a29d47c8d238c467e6ab0f55404cff28bc0c3a..ae6a3e9144c5823855a8dd7c93799d4bebb1e477 100644 --- a/gandi-ddns.py +++ b/gandi-ddns.py @@ -3,6 +3,7 @@ import sys import os import requests import json +import ipaddress config_file = "config.txt" @@ -20,7 +21,12 @@ def get_ip(): print('Failed to retrieve external IP. Server responded with status_code: %d' % result.status_code) sys.exit(2) - return r.text + 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 @@ -79,7 +85,7 @@ def main(): 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()[0:-1] + external_ip = get_ip() print("external IP is: %s" % external_ip) #Prepare record