-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBitHound.py
133 lines (113 loc) · 4.34 KB
/
BitHound.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import os
import webbrowser
import smtplib
import time
import imaplib
import email
from email.mime.text import MIMEText
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
from email.header import decode_header
import ping3
import matplotlib.pyplot as plt
# Configuration
MONITORING = True
EMAIL_SERVER = 'smtp.gmail.com' # Replace with your email provider's SMTP server
SMTP_PORT = 587 # May be 465 for SSL
SENDER_EMAIL = 'sender_email'
PASSWORD = 'password'
RECIPIENT_EMAIL = '[email protected]'
TARGET_HOST = 'www.google.com' # Website or IP for ping test
TODAY = time.strftime('%Y-%m-%d')
# Data tracking
PING_RESULTS = []
TIMESTAMPS = []
def check_internet_and_record():
"""Checks internet connectivity and stores results."""
result = ping3.ping(TARGET_HOST)
# Handle missing results gracefully
if result is not False:
PING_RESULTS.append(result)
else:
PING_RESULTS.append(0) # Indicates connection issue
TIMESTAMPS.append(time.strftime('%H:%M'))
def send_report():
"""Generates a plot and sends the email report."""
hours = []
for i in range(len(TIMESTAMPS)):
if TIMESTAMPS[i].split(':')[0] not in hours:
hours.append(TIMESTAMPS[i].split(':')[0])
else:
hours.append(' ')
plt.figure(figsize=(8, 4)) # Adjust figure size as needed
plt.plot(hours, PING_RESULTS)
plt.xlabel('time')
plt.ylabel('Ping (ms)')
plt.title(f'Internet Connectivity Report - {TODAY}')
plt.grid(True)
plt.savefig('connectivity_report.png') # Save plot as an image
# Construct email
msg = MIMEMultipart()
msg['Subject'] = 'Daily Internet Connectivity Report'
msg['From'] = SENDER_EMAIL
msg['To'] = RECIPIENT_EMAIL
text = MIMEText("Please find attached the daily internet connectivity report.")
msg.attach(text)
with open(f'connectivity_report.png', 'rb') as f:
attachment = MIMEApplication(f.read(), _subtype='png')
attachment.add_header('Content-Disposition', 'attachment', filename=f'connectivity_report.png')
msg.attach(attachment)
# Send the email
with smtplib.SMTP(EMAIL_SERVER, SMTP_PORT) as smtp:
smtp.starttls()
smtp.login(SENDER_EMAIL, PASSWORD)
smtp.sendmail(SENDER_EMAIL, RECIPIENT_EMAIL, msg.as_string())
# Main loop
def main(PING_RESULTS, TIMESTAMPS):
while MONITORING == True:
check_internet_and_record()
# Adjust the time for the end of the day reporting
current_hour = int(time.strftime('%H'))
if current_hour == 23: # Send report around 11 PM or when ping is too high
send_report()
# if uptime.log exists, append data to it
with open(f'uptime_{TODAY}.log', 'a') as f:
for i in range(len(TIMESTAMPS)):
f.write(TIMESTAMPS[i] + ',' + str(PING_RESULTS[i]) + '\n')
PING_RESULTS = [] # Reset data for the next day
TIMESTAMPS = []
time.sleep(300) # Wait for 5 minutes
emailscanner(PING_RESULTS, TIMESTAMPS)
# Start main loop
def emailscanner(PING_RESULTS, TIMESTAMPS):
while True:
mail = imaplib.IMAP4_SSL(EMAIL_SERVER, 993)
mail.login(SENDER_EMAIL, PASSWORD)
mail.select("inbox")
result, data = mail.uid("search", None, "ALL")
mails = len(data[0].split())
# Find all emails in the inbox
status, messages = mail.search(None, 'ALL')
latest_email_uid = data[0].split()[-1]
result, email_data = mail.uid("fetch", latest_email_uid, "(RFC822)")
raw_email = email_data[0][1]
raw_email_string = raw_email.decode("utf-8")
email_message = email.message_from_string(raw_email_string)
subject = email_message["subject"]
print (subject)
if subject == "start":
main(PING_RESULTS, TIMESTAMPS)
elif subject == "stop":
MONITORING = False
elif subject == "reset":
os.system(f"mv uptime_{TODAY} logs/uptime_{TODAY}.log")
PING_RESULTS = []
TIMESTAMPS = []
elif subject == "scan":
check_internet_and_record()
send_report()
elif subject == "clear":
os.system("rm -rf logs/ && mkdir logs")
else:
continue
emailscanner(PING_RESULTS, TIMESTAMPS)