Skip to content

Instantly share code, notes, and snippets.

@fredericrous
Last active April 1, 2018 22:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fredericrous/cee9ec3b7caff235b74e2fe67ff7e700 to your computer and use it in GitHub Desktop.
Save fredericrous/cee9ec3b7caff235b74e2fe67ff7e700 to your computer and use it in GitHub Desktop.
Update gandi domain with dynamic IP from orange (livebox3). who needs dyndns =B. Usage: ./gandiDynamicIP.py
#!/usr/bin/env python
#
# Update ip of a domain like dyndns would but on gandi
#
# author: Zougi
import urllib, urllib2, json
from livebox3IP import externalIP
def getIP(domain, apikey):
opener = urllib2.build_opener(urllib2.HTTPHandler)
data = json.dumps({ 'rrset_ttl': '300', 'rrset_values': [ ip ] })
headers = {"X-Api-Key": apikey, "Content-Type": "application/json"}
url = "https://dns.api.gandi.net/api/v5/domains/%s/records/%s/%s" % (domain, urllib.quote('@'), 'A')
request = urllib2.Request(url, data, headers)
request.get_method = lambda: 'GET'
conn = opener.open(request)
assert conn.code == 200, "request failed with %r" % conn.code
data = conn.read()
datas = json.loads(data)
conn.close()
return datas['rrset_values'][0]
def updateIP(domain, ip, apikey):
opener = urllib2.build_opener(urllib2.HTTPHandler)
data = json.dumps({ 'rrset_ttl': '300', 'rrset_values': [ ip ] })
headers = {"X-Api-Key": apikey, "Content-Type": "application/json"}
url = "https://dns.api.gandi.net/api/v5/domains/%s/records/%s/%s" % (domain, urllib.quote('@'), 'A')
request = urllib2.Request(url, data, headers)
request.get_method = lambda: 'PUT'
conn = opener.open(request)
assert conn.code == 201, "request failed with %r" % conn.code
conn.close()
if __name__ == "__main__":
import sys, argparse
parser = argparse.ArgumentParser()
parser.add_argument('--gandiApiKey', nargs=1, help='the API KEY to call gandi REST services')
parser.add_argument('--liveboxPass', nargs=1, help='the password to livebox administration')
parser.add_argument('domain', nargs=1, help='the domain to update')
args = parser.parse_args()
if args.domain == None or args.gandiApiKey == None or args.liveboxPass == None:
print "missing parameter..."
parser.print_help()
sys.exit(1)
else:
domain = args.domain[0]
apikey = args.gandiApiKey[0]
password = args.liveboxPass[0]
ip = externalIP(password)
print "current ip is %s" % ip
gandiIp = getIP(domain, apikey)
print "ip on gandi is %s" % gandiIp
if gandiIp != ip:
updateIP(domain, ip, apikey)
print "ip updated to %r" % ip
else:
print "ip already up to date"
#!/usr/bin/env python
#
# Get IP from livebox 3
#
# author: Zougi
import urllib, urllib2, cookielib, json
def externalIP(password):
cj = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
data = json.dumps({ 'username': 'admin', 'password': password })
headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "*/*"}
request = urllib2.Request("http://192.168.1.1/authenticate?username=admin&password=%s" % urllib.quote(password), data, headers)
conn = opener.open(request)
assert conn.code == 200, "request failed with %r" % conn.code
conn.close()
request = urllib2.Request("http://192.168.1.1/sysbus/NMC:getWANStatus", '{"parameters":{}}')
conn = opener.open(request)
data = conn.read()
datas = json.loads(data)
conn.close()
return datas['data']['IPAddress']
if __name__ == "__main__":
import sys
print externalIP(sys.argv[1])
@fredericrous
Copy link
Author

fredericrous commented Apr 1, 2018

Crontab example:

* */15 * * * /root/gandiDynamicIP.py mydomain.io --liveboxPass 'myadministrationpassword' --gandiApiKey apikeyfromgandi > /dev/null

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment