#!/app/python3/bin/python3 # vplesnila 2019-06-24: creation # vplesnila 2019-06-25: build self.subdomains_list # vplesnila 2019-07-01: finalize update subdomains procedure and email sending import os import smtplib from email.message import EmailMessage from email.mime.text import MIMEText import socket import json import requests import logging # LOGGING initialization function def start_logging(logfile): logger = logging.getLogger(__name__) logger.setLevel(logging.INFO) # create a file handler handler = logging.FileHandler(logfile) handler.setLevel(logging.INFO) # create a logging format formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') handler.setFormatter(formatter) # add the handlers to the logger logger.addHandler(handler) return logger class DomainUpdater: def __init__(self, rootdir): self.rootdir = rootdir # Load configuration with open(self.rootdir + "/" + "DomainUpdater.conf", "r") as f: self.config = json.load(f) # Get current IP r = requests.get(self.config["get_current_ip_api_url"]) self.current_ip = r.json()["ip"] logger.info("__BEGIN_BATCH__") logger.info("Current IP is " + self.current_ip) # Build subdomain list as a ist of (subdomain name, domain_id, subdomain_id) self.subdomains_list = [] self.auth = (self.config["dnsmanager_id"], self.config["dnsmanager_key"]) self.url_base = self.config["dnsmanager_api_url_base"] # Get domains dictionary url_end = "/user/domains" r = requests.get(self.url_base + url_end, auth = self.auth) domain_dict = r.json()["results"] for domain in domain_dict: domain_name = domain["domain"] domain_id = domain["id"] url_end = "/user/domain/" + str(domain["id"]) + "/records" r = requests.get(self.url_base + url_end, auth = self.auth) records_dict = r.json()["results"] for record_dict in records_dict: if record_dict["type"] == "A": # Subdomain subdomain_id = record_dict["id"] subdomain_name = record_dict["name"] subdomain_ip = record_dict["content"] fqdn = "%s.%s" % (subdomain_name, domain_name) record = (fqdn, domain_id, subdomain_id, subdomain_ip) self.subdomains_list.append(record) return def send_email_new_ip(self): msg = EmailMessage() msg["Subject"] = "Your public IP changed" msg["From"] = "domain-updater@databasepro.fr" msg["To"] = "vplesnila@gmail.com" body = """ Hello, You have a new public IP: %s Following subdomains has been updated: %s -------------- Domain Updater """ % (self.current_ip, "\n".join(self.updated_subdomain_list)) msg.set_content(body) s = smtplib.SMTP("localhost") s.send_message(msg) s.quit() logger.info("Email sent to " + msg["To"]) return def check_subdomains(self): self.updated_subdomain_list=[] for record in self.subdomains_list: (fqdn, domain_id, subdomain_id, subdomain_ip) = record if (subdomain_ip == self.current_ip): logger.info(fqdn + " already set to " + subdomain_ip + ", nothing to do") else: logger.info("Updatting " + fqdn + " with the new IP value " + self.current_ip) self.update_subdomain(domain_id, subdomain_id) self.updated_subdomain_list.append(fqdn) logger.info("__END_BATCH__") return def update_subdomain(self, domain_id, subdomain_id): url_end = "/user/domain/" + str(domain_id) + "/record/" + str(subdomain_id) data = json.dumps({"id":subdomain_id, "content":self.current_ip}) headers = { 'Content-Type': 'application/json'} r = requests.put(self.url_base + url_end, auth = self.auth, data = data, headers=headers) return if __name__ == "__main__": script_path = os.path.dirname(os.path.abspath(__file__)) script_name = os.path.basename(__file__) logger = start_logging(script_path + '/DomainUpdater.log') domainupdater = DomainUpdater(script_path) domainupdater.check_subdomains() if len(domainupdater.updated_subdomain_list) > 0: domainupdater.send_email_new_ip()