2026-03-12 20:51:59
This commit is contained in:
0
python/01.py
Normal file
0
python/01.py
Normal file
128
python/DomainUpdater/DomainUpdater.2019-07-01
Normal file
128
python/DomainUpdater/DomainUpdater.2019-07-01
Normal file
@@ -0,0 +1,128 @@
|
||||
#!/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()
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
9
python/DomainUpdater/DomainUpdater.conf
Normal file
9
python/DomainUpdater/DomainUpdater.conf
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"email_from": "domainupdater@databasepro.eu",
|
||||
"email_to": "vplesnila@gmail.com",
|
||||
"get_current_ip_api_url": "https://api.ipify.org?format=json",
|
||||
"dnsmanager_api_url_base":"https://app.dnsmanager.io/api/v1",
|
||||
"dnsmanager_id":"9422ac9d-2c62-4967-ae12-c1d15bbbe200",
|
||||
"dnsmanager_key":"I9HV2Jqp1gFqMuic3zPRYW5guSQEvoyy",
|
||||
"subdomain_list":"ssh.databasepro.fr,code.databasepro.fr,sabnzbd.databasepro.eu,sabnzbd.databasepro.fr,public.databasepro.fr,support.databasepro.fr"
|
||||
}
|
||||
8
python/DomainUpdater/DomainUpdater.conf.2020-12-23
Normal file
8
python/DomainUpdater/DomainUpdater.conf.2020-12-23
Normal file
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"email_from": "domainupdater@databasepro.fr",
|
||||
"email_to": "vplesnila@gmail.com",
|
||||
"get_current_ip_api_url": "https://api.ipify.org?format=json",
|
||||
"dnsmanager_api_url_base":"https://app.dnsmanager.io/api/v1",
|
||||
"dnsmanager_id":"9422ac9d-2c62-4967-ae12-c1d15bbbe200",
|
||||
"dnsmanager_key":"I9HV2Jqp1gFqMuic3zPRYW5guSQEvoyy"
|
||||
}
|
||||
9
python/DomainUpdater/DomainUpdater.conf.2021-01-15
Normal file
9
python/DomainUpdater/DomainUpdater.conf.2021-01-15
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"email_from": "domainupdater@databasepro.fr",
|
||||
"email_to": "vplesnila@gmail.com",
|
||||
"get_current_ip_api_url": "https://api.ipify.org?format=json",
|
||||
"dnsmanager_api_url_base":"https://app.dnsmanager.io/api/v1",
|
||||
"dnsmanager_id":"9422ac9d-2c62-4967-ae12-c1d15bbbe200",
|
||||
"dnsmanager_key":"I9HV2Jqp1gFqMuic3zPRYW5guSQEvoyy",
|
||||
"subdomain_list":"None.databasepro.fr,ssh.databasepro.fr"
|
||||
}
|
||||
147
python/DomainUpdater/DomainUpdater.py
Normal file
147
python/DomainUpdater/DomainUpdater.py
Normal file
@@ -0,0 +1,147 @@
|
||||
#!/usr/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
|
||||
# vplesnila 2020-12-24: add subdomain list in config file to allow updating only a subset of dnsmanager.io registered subdomains
|
||||
|
||||
import os
|
||||
import socket
|
||||
import json
|
||||
import requests
|
||||
import logging
|
||||
import smtplib
|
||||
from email.message import EmailMessage
|
||||
from email.mime.text import MIMEText
|
||||
|
||||
|
||||
# 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("Current public 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"]
|
||||
|
||||
# Get fqdn list
|
||||
self.fqdn_list = self.config["subdomain_list"].split(",")
|
||||
logger.info("Subdomains list: " + ",".join(self.fqdn_list))
|
||||
|
||||
self.fqdn_to_update = []
|
||||
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)
|
||||
if (fqdn in self.fqdn_list):
|
||||
self.fqdn_to_update.append(fqdn)
|
||||
self.subdomains_list.append(record)
|
||||
|
||||
logger.info("Updating subdomain(s): " + ",".join(self.fqdn_to_update))
|
||||
return
|
||||
|
||||
|
||||
def send_email_new_ip(self):
|
||||
SMTPserver = 'smtp.orange.fr'
|
||||
sender = 'Domain-Updater@databasepro.fr'
|
||||
destination = ['vplesnila@gmail.com']
|
||||
USERNAME = "plesnila.valeriu@orange.fr"
|
||||
PASSWORD = "ch1tzch1tz"
|
||||
|
||||
msg = EmailMessage()
|
||||
msg["Subject"] = "Your public IP changed"
|
||||
msg["From"] = sender
|
||||
msg["To"] = destination
|
||||
body = """
|
||||
Hello,
|
||||
You have a new public IP: %s
|
||||
Following subdomains has been updated: %s
|
||||
--------------
|
||||
Domain-Updater
|
||||
""" % (self.current_ip, ", ".join(self.updated_subdomain_list))
|
||||
msg.set_content(body)
|
||||
s = smtplib.SMTP(host = SMTPserver, port= 587)
|
||||
s.set_debuglevel(False)
|
||||
s.login(USERNAME, PASSWORD)
|
||||
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("Updating " + fqdn + " with the new IP value " + self.current_ip)
|
||||
self.update_subdomain(domain_id, subdomain_id)
|
||||
self.updated_subdomain_list.append(fqdn)
|
||||
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')
|
||||
logger.info("__BEGIN_BATCH__")
|
||||
domainupdater = DomainUpdater(script_path)
|
||||
domainupdater.check_subdomains()
|
||||
if len(domainupdater.updated_subdomain_list) > 0:
|
||||
domainupdater.send_email_new_ip()
|
||||
logger.info("__END_BATCH__")
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
126
python/DomainUpdater/DomainUpdater.py.2020-12-23
Normal file
126
python/DomainUpdater/DomainUpdater.py.2020-12-23
Normal file
@@ -0,0 +1,126 @@
|
||||
#!/app/python/current_version/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("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, ", ".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("Updating " + fqdn + " with the new IP value " + self.current_ip)
|
||||
self.update_subdomain(domain_id, subdomain_id)
|
||||
self.updated_subdomain_list.append(fqdn)
|
||||
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')
|
||||
logger.info("__BEGIN_BATCH__")
|
||||
domainupdater = DomainUpdater(script_path)
|
||||
domainupdater.check_subdomains()
|
||||
if len(domainupdater.updated_subdomain_list) > 0:
|
||||
domainupdater.send_email_new_ip()
|
||||
logger.info("__END_BATCH__")
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
136
python/DomainUpdater/DomainUpdater.py.2021-12-05
Normal file
136
python/DomainUpdater/DomainUpdater.py.2021-12-05
Normal file
@@ -0,0 +1,136 @@
|
||||
#!/usr/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
|
||||
# vplesnila 2020-12-24: add subdomain list in config file to allow updating only a subset of dnsmanager.io registered subdomains
|
||||
|
||||
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("Current public 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"]
|
||||
|
||||
# Get fqdn list
|
||||
self.fqdn_list = self.config["subdomain_list"].split(",")
|
||||
logger.info("Subdomains list: " + ",".join(self.fqdn_list))
|
||||
|
||||
self.fqdn_to_update = []
|
||||
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)
|
||||
if (fqdn in self.fqdn_list):
|
||||
self.fqdn_to_update.append(fqdn)
|
||||
self.subdomains_list.append(record)
|
||||
|
||||
logger.info("Updating subdomain(s): " + ",".join(self.fqdn_to_update))
|
||||
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, ", ".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("Updating " + fqdn + " with the new IP value " + self.current_ip)
|
||||
self.update_subdomain(domain_id, subdomain_id)
|
||||
self.updated_subdomain_list.append(fqdn)
|
||||
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')
|
||||
logger.info("__BEGIN_BATCH__")
|
||||
domainupdater = DomainUpdater(script_path)
|
||||
domainupdater.check_subdomains()
|
||||
if len(domainupdater.updated_subdomain_list) > 0:
|
||||
domainupdater.send_email_new_ip()
|
||||
logger.info("__END_BATCH__")
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
BIN
python/DomainUpdater/__pycache__/DomainUpdater.cpython-36.pyc
Normal file
BIN
python/DomainUpdater/__pycache__/DomainUpdater.cpython-36.pyc
Normal file
Binary file not shown.
2
python/Readme.md
Normal file
2
python/Readme.md
Normal file
@@ -0,0 +1,2 @@
|
||||
Directory for Python scripts
|
||||
----------------------------
|
||||
15
python/scr_cod_dec/code.py
Normal file
15
python/scr_cod_dec/code.py
Normal file
@@ -0,0 +1,15 @@
|
||||
#!/home/oracle/p3/bin/python
|
||||
|
||||
import os
|
||||
|
||||
k_scriptdir = "/mnt/yavin4/tmp/_oracle_/gitlab2/oracle/star"
|
||||
scriptlist = sorted(os.listdir(k_scriptdir))
|
||||
for script in scriptlist:
|
||||
if script.endswith(".sql"):
|
||||
scriptlen = len(script)
|
||||
script_underline = "".join("~" for i in range(scriptlen +4))
|
||||
print(f"{script_underline}\n~ {script} ~\n{script_underline}")
|
||||
with open(k_scriptdir + "/" + script) as f:
|
||||
file_contents = f.read().splitlines()
|
||||
for script_line in file_contents:
|
||||
print(f"\t{script_line}")
|
||||
34
python/scr_cod_dec/decode.py
Normal file
34
python/scr_cod_dec/decode.py
Normal file
@@ -0,0 +1,34 @@
|
||||
#!/home/oracle/p3/bin/python
|
||||
|
||||
import re
|
||||
import fileinput
|
||||
|
||||
ifile = "1-3.txt"
|
||||
odir = "./unpack/"
|
||||
|
||||
fc_cnt = sum(1 for line in fileinput.input(ifile))
|
||||
with open(ifile) as f:
|
||||
fc = f.read().splitlines()
|
||||
|
||||
i = 0
|
||||
while i<fc_cnt-2:
|
||||
l1 = fc[i]
|
||||
l2 = fc[i+1]
|
||||
l3 = fc[i+2]
|
||||
if l1.startswith("~") and l1.endswith("~") and l2.startswith("~ ") and l2.endswith(".sql ~") and l3.startswith("~") and l3.endswith("~"):
|
||||
# Line 2 contains the script name
|
||||
sname = l2.replace("~","").replace(" ","")
|
||||
scontents = ""
|
||||
j = i + 4
|
||||
while j<fc_cnt:
|
||||
l = fc[j]
|
||||
if l.startswith("~") and l.endswith("~"):
|
||||
# End of script
|
||||
with open(f"{odir}/{sname}", "w") as f:
|
||||
f.write(scontents)
|
||||
break
|
||||
else:
|
||||
# Line is part of current script body
|
||||
j += 1
|
||||
scontents = scontents + l.lstrip("\t") + "\n"
|
||||
i += 2
|
||||
Reference in New Issue
Block a user