Private GIT

Skip to content
Snippets Groups Projects
Commit d6949f7a authored by arjuan's avatar arjuan
Browse files

add ipaddress validation

parent 1d10eade
Branches
No related tags found
No related merge requests found
...@@ -5,3 +5,7 @@ http://doc.livedns.gandi.net/ ...@@ -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). 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). 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)
...@@ -3,6 +3,7 @@ import sys ...@@ -3,6 +3,7 @@ import sys
import os import os
import requests import requests
import json import json
import ipaddress
config_file = "config.txt" config_file = "config.txt"
...@@ -20,7 +21,12 @@ def get_ip(): ...@@ -20,7 +21,12 @@ def get_ip():
print('Failed to retrieve external IP. Server responded with status_code: %d' % result.status_code) print('Failed to retrieve external IP. Server responded with status_code: %d' % result.status_code)
sys.exit(2) 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): def read_config(config_path):
#Read configuration file #Read configuration file
...@@ -79,7 +85,7 @@ def main(): ...@@ -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")) url = '%sdomains/%s/records/%s/A' % (config.get(section, "api"), config.get(section, "domain"), config.get(section, "a_name"))
print(url) print(url)
#Discover External IP #Discover External IP
external_ip = get_ip()[0:-1] external_ip = get_ip()
print("external IP is: %s" % external_ip) print("external IP is: %s" % external_ip)
#Prepare record #Prepare record
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment