-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
61 lines (54 loc) · 1.9 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import mysql.connector
from mysql.connector import Error
from datetime import datetime, timedelta
import smtplib
import ssl
from decouple import config
#SMTP variables
smtpServer = config('SMTPSERVER')
port = config('SMTPPORT')
senderEmail = config('SMTPSENDEREMAIL')
recieverEmail = [config('SMTPRECEIVEREMAIL'))]
subject = "WARNING: RADIUS MYSQL Replication"
message = f"From: {config('FROM_HEADER')}\nSubject:{subject}\n\n"
context = ssl.create_default_context()
#initialize SMTP
server = smtplib.SMTP(smtpServer, port)
server.starttls(context=context)
try:
#Make sure to get your connection credentials correct
db = mysql.connector.connect(
host=config('SQLSERVER')
database=config('DBNAME'),
user=config('DBUSER'),
password=config('DBPASSWORD'),
auth_plugin='mysql_native_password',
)
rejected = []
if db.is_connected():
myCursor = db.cursor(buffered=True)
myCursor.execute("CALL rts_CheckAccessRejects();")
results = myCursor.fetchall()
outcome = []
message+=f"Access-Rejects are appearing on RADIUS. Please check the RADIUS server. Flagged Machines: \n"
#dbTime = results[0][-1]
for i in results:
outcome.append([i[1],i[3]])
for i in outcome:
if i[1] == "Access-Reject":
print("access is rejected here")
flagged_machine = f"{i[0]} \n"
message += flagged_machine
rejected.append(flagged_machine)
if rejected != []:
print(rejected)
#server.sendmail(senderEmail, recieverEmail, message)
server.quit()
db.close()
except Error as e:
print("Error while connecting to MySQL", e)
finally:
if datetime.now().strftime("%H:%M") == "08:00":
subject = "RADIUS Access-Reject Test"
message+= "RADIUS Access-Reject Monitor is up and running"
server.sendmail(senderEmail, recieverEmail, message)