From 6baad4ad6052455ff0e25ecf6b95da66ddcdc377 Mon Sep 17 00:00:00 2001 From: Mikatech Date: Fri, 30 Jul 2021 23:49:01 +0200 Subject: [PATCH] [New Feature] send logs with email --- .gitignore | 1 + .vscode/settings.json | 3 --- src/config.json | 5 +++++ src/software/logsManager.py | 42 ++++++++++++++++++++++++++++++++++++- 4 files changed, 47 insertions(+), 4 deletions(-) delete mode 100644 .vscode/settings.json create mode 100644 src/config.json diff --git a/.gitignore b/.gitignore index 22153fd..5994ce3 100644 --- a/.gitignore +++ b/.gitignore @@ -134,3 +134,4 @@ dmypy.json Datasets/output_http_csic_2010_weka_with_duplications_RAW-RFC2616_escd_v02_full.csv Execution/Dataset_test.csv +.github \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json deleted file mode 100644 index 73a6703..0000000 --- a/.vscode/settings.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "python.pythonPath": "/home/evan/anaconda3/envs/PocEnv/bin/python" -} \ No newline at end of file diff --git a/src/config.json b/src/config.json new file mode 100644 index 0000000..8c20fa4 --- /dev/null +++ b/src/config.json @@ -0,0 +1,5 @@ +{ + "fromAddr": "sender", + "passwordAddr": "password of sender", + "toAddr": "receiver" +} \ No newline at end of file diff --git a/src/software/logsManager.py b/src/software/logsManager.py index 52fb42d..87c1d35 100644 --- a/src/software/logsManager.py +++ b/src/software/logsManager.py @@ -1 +1,41 @@ -import json \ No newline at end of file +import json +from datetime import datetime +import smtplib +from email.mime.multipart import MIMEMultipart +from email.mime.text import MIMEText +from email.mime.base import MIMEBase +from email import encoders + +def logsManager(self): + def __init__(self, path: str) -> None: + self.__path = path + with open(self.__path, "r").read() as config: + config = json.load(config) + self.__from_addr = config['fromAddr'] + self.__password_addr = config['passwordAddr'] + self.__to_addr = config['toAddr'] + + def send_mail(self, msg: str) -> None: + now = datetime.now() + current_time = now.strftime("%d-%m-%Y %Hh%Mm%Ss%f")[:-3] + subject = "Sharkticon anomaly detected" + body = "Date : " + current_time + "ms\nLog : " + msg + "\n\nUne anomalie nécessite une " \ + "verification\n" + msg = MIMEMultipart() + msg['From'] = self.__from_addr + msg['To'] = self.__to_addr + msg['Subject'] = subject + msg.attach(MIMEText(body, 'plain')) + s = smtplib.SMTP('smtp.gmail.com', 587) + s.starttls() + try: + s.login(self.__from_addr, self.__password_addr) + except: + print("Error : your mail config isn't valid") + return + text = msg.as_string() + s.sendmail(self.__from_addr, self.__to_addr, text) + s.quit() + + + \ No newline at end of file