fix & cleanup
This commit is contained in:
parent
f381cc46d8
commit
b012a6b7dd
1 changed files with 19 additions and 19 deletions
|
@ -22,23 +22,23 @@ can be provided in variety of ways:
|
|||
'''
|
||||
-or in the ENVIRONMENT
|
||||
-or in a ovh.conf file
|
||||
in which cases we can call with no argument (the ovh module gets the credentials on its own):
|
||||
in which cases we can call with no argument (the ovh module gets the credentials on its own):
|
||||
'''
|
||||
client = ovh.Client()
|
||||
|
||||
default_ttl = 600 # seconds
|
||||
default_ttl = 600 # seconds
|
||||
# ttl = how long will a DNS server cache the value before checking it at the Registrar. Longer value yields faster name resolution most of the time, but less frequent updates
|
||||
records_changed = 0
|
||||
records_changed = 0
|
||||
# list of hosts (=subdomain.domain.tld) to update, each a dictionnary with at least "domain" and "subdomain" defined
|
||||
hosts = [
|
||||
{
|
||||
{
|
||||
"domain": "mydomain.tld", # Required
|
||||
"subdomain": "www", # Required. Explicit subdomain or empty string "" (for @) or "*" for wildcard
|
||||
#"ipv6": any_value_except_False # Optional : maintain corresponding record, when possible
|
||||
"ipv4": False, #explicitly disable modifiying ipv4 (A) records, even if public IPV4 exists (a possibly erroneous record would be left as-is)
|
||||
#"ttl": 60 # optional : if 'ttl' in specified in host, overrides the global default value
|
||||
},
|
||||
{
|
||||
{
|
||||
"domain": "otherdomain.tld",
|
||||
"subdomain": ""
|
||||
# 'ipv4' and 'ipv6' are not listed : automatically maintain any/both records, according to availability
|
||||
|
@ -46,30 +46,30 @@ hosts = [
|
|||
]
|
||||
|
||||
checkDNS_interval_hrs = 12.1 # when the saved IP addresses are old, check the DNS record, even if the addresses did not change
|
||||
#save last known address in local file.
|
||||
current_ip_file = "/tmp/current_ip.json"
|
||||
#save last known address in local file.
|
||||
current_ip_file = "/tmp/current_ip.json"
|
||||
|
||||
|
||||
def send_email(msg, sender = 'no_reply@mydomain.com', receiver = 'admin@mydomain.com')
|
||||
def send_email(msg, sender = 'no_reply@mydomain.com', receiver = 'admin@mydomain.com') :
|
||||
import smtplib
|
||||
|
||||
try :
|
||||
smtpObj = smtplib.SMTP('localhost')
|
||||
smtpObj.sendmail(sender, receiver,
|
||||
"From: {}\nTo: {}\nSubject: DNS Update problem\n\n{}\n".format(sender, receiver, msg)
|
||||
)
|
||||
)
|
||||
except smtplib.SMTPException:
|
||||
print( timestamp()," : Error unable to send email")
|
||||
|
||||
def get_current_ip(v = 4):
|
||||
url = 'https://api6.ipify.org' if v == 6 else 'https://api.ipify.org'
|
||||
try :
|
||||
try :
|
||||
r = requests.get(url, timeout=5.0)
|
||||
except requests.exceptions.RequestException as e:
|
||||
#print("failed getting ipv{} address because {} occurred".format(v, str(e)))
|
||||
return False
|
||||
return r.text if r.status_code == requests.codes.ok else False
|
||||
|
||||
|
||||
def timestamp() :
|
||||
return time.asctime( time.localtime(time.time()) )
|
||||
|
||||
|
@ -112,9 +112,9 @@ def update_record(domain, subdomain, new_ip, _ttl = 600):
|
|||
return
|
||||
else :
|
||||
#print('updating to ', new_ip)
|
||||
result = client.put(path,
|
||||
subDomain = subdomain,
|
||||
target = new_ip,
|
||||
result = client.put(path,
|
||||
subDomain = subdomain,
|
||||
target = new_ip,
|
||||
ttl = _ttl
|
||||
)
|
||||
client.post('/domain/zone/{}/refresh'.format(domain))
|
||||
|
@ -124,11 +124,11 @@ def update_record(domain, subdomain, new_ip, _ttl = 600):
|
|||
if new_ip != result['target'] :
|
||||
records_changed -= 1
|
||||
raise Exception("Error updating {}.{} with {}".format(subdomain,domain,new_ip))
|
||||
|
||||
|
||||
|
||||
|
||||
def delete_record(domain, subdomain, typ):
|
||||
"""
|
||||
if it exists, delete an A or AAAA record
|
||||
if it exists, delete an A or AAAA record
|
||||
(because the corresponding IP is not available)
|
||||
"""
|
||||
#print("checking record {} for {}.{}".format(typ,subdomain,domain))
|
||||
|
@ -161,7 +161,7 @@ if current_ipv4 or current_ipv6 : #we could get at least one address
|
|||
try :
|
||||
need_update = (old_ipv4 != current_ipv4) or (old_ipv6 != current_ipv6) or ((old_time - time.time()) > 3600.0 * checkDNS_interval_hrs)
|
||||
except : #old values do not exist, we must check the records
|
||||
need_update = True
|
||||
need_update = True
|
||||
if need_update :
|
||||
try :
|
||||
for host in hosts :
|
||||
|
@ -186,7 +186,7 @@ if current_ipv4 or current_ipv6 : #we could get at least one address
|
|||
#print("Not touching AAAA record for {}.{}, as instructed".format(subdomain, domain))
|
||||
pass
|
||||
#all hosts records have been updated without errors, log change and save current addresses
|
||||
print("{} : new addresses {} ; {} -- {} records updates".format(timestamp(), current_ipv4, current_ipv6, records_changed)
|
||||
print("{} : new addresses {} ; {} -- {} records updates".format(timestamp(), current_ipv4, current_ipv6, records_changed))
|
||||
with open(current_ip_file, 'w') as f:
|
||||
json.dump([time.time(), current_ipv4, current_ipv6],f)
|
||||
except Exception as e: #some error occured (API down, keys expired...?),
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue