commit
a226ae4cd5
@ -0,0 +1,76 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
## imports
|
||||
import json
|
||||
import ovh
|
||||
import requests
|
||||
import os, sys
|
||||
import logging
|
||||
import logging.handlers
|
||||
from dotenv import load_dotenv
|
||||
|
||||
my_logger = logging.getLogger('MyLogger')
|
||||
my_logger.setLevel(logging.DEBUG)
|
||||
|
||||
handler = logging.handlers.SysLogHandler(address = '/dev/log')
|
||||
formatter = logging.Formatter("dyndns-update: %(message)s")
|
||||
handler.setFormatter(formatter)
|
||||
|
||||
my_logger.addHandler(handler)
|
||||
|
||||
|
||||
env_path = os.path.abspath ( os.path.join ( os.curdir, os.path.relpath('.env') ) )
|
||||
load_dotenv(verbose=True, dotenv_path=env_path)
|
||||
|
||||
## constants
|
||||
client = ovh.Client(
|
||||
endpoint=os.getenv('ENDPOINT'), # Endpoint of API OVH Europe (List of available endpoints)
|
||||
application_key=os.getenv('APP_KEY'), # Application Key
|
||||
application_secret=os.getenv('SECRET'), # Application Secret
|
||||
consumer_key=os.getenv('CONSUMER_KEY'), # Consumer Key
|
||||
)
|
||||
|
||||
TMPFILE = '/tmp/lastIP'
|
||||
|
||||
def get_ip():
|
||||
r = requests.get('http://ipinfo.io')
|
||||
return (r.json()['ip'])
|
||||
|
||||
def get_record_id(zone,subdomain):
|
||||
result = client.get('/domain/zone/' + zone + '/record', fieldType='A', subDomain=subdomain )
|
||||
# print (json.dumps(result, indent=4))
|
||||
|
||||
return result[0]
|
||||
|
||||
def set_record(zone, subdomain, ip):
|
||||
id=get_record_id(zone,subdomain)
|
||||
|
||||
result = client.put('/domain/zone/joefix.it/record/' + str(id), subDomain=subdomain, target=ip, ttl=60)
|
||||
return result
|
||||
|
||||
|
||||
if len(sys.argv) > 2:
|
||||
if os.path.exists(TMPFILE):
|
||||
with open(TMPFILE) as file:
|
||||
try:
|
||||
lastIP=file.read()
|
||||
except:
|
||||
my_logger.critical('error reading ' + TMPFILE)
|
||||
else:
|
||||
lastIP = ''
|
||||
|
||||
currentIP = get_ip()
|
||||
|
||||
if lastIP == currentIP:
|
||||
my_logger.debug ('IP has not changed since last call')
|
||||
exit()
|
||||
|
||||
|
||||
if (set_record (sys.argv[1], sys.argv[2], currentIP) == None ):
|
||||
my_logger.debug('IP of ' + sys.argv[2] + '.' + sys.argv[1] + ' successfully changed to ' + currentIP)
|
||||
|
||||
with open(TMPFILE, 'w') as file:
|
||||
file.write(currentIP)
|
||||
|
||||
else:
|
||||
print('Updates IP record of SUBDOMAIN in ZONE to current IP from ipinfo.io\n\nUsage:\n' + sys.argv[0] + ' ZONE SUBDOMAIN')
|
@ -0,0 +1,5 @@
|
||||
ENDPOINT='ovh-eu'
|
||||
APP_KEY='xxx'
|
||||
SECRET='xxx'
|
||||
CONSUMER_KEY='xxx'
|
||||
|
Loading…
Reference in new issue