Skip to content

Commit 14af890

Browse files
authored
Add files via upload
1 parent e73e378 commit 14af890

File tree

1 file changed

+104
-0
lines changed

1 file changed

+104
-0
lines changed

Network_Notification.py

+104
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
import subprocess
2+
import threading
3+
import configparser
4+
from colorama import init
5+
from colorama import Fore, Back, Style
6+
init()
7+
config = configparser.ConfigParser()
8+
config.read('config.ini')
9+
10+
found = False
11+
foundagain = False
12+
mailsent = False
13+
failed = 0
14+
15+
def sniff():
16+
global found, foundagain, mailsent, failed
17+
address = config['GENERAL']['IP_OF_DEVICE']
18+
res = subprocess.call(['ping', '-n', '1', "-w", "200", address])
19+
if res == 0:
20+
failed = 0
21+
print (Fore.GREEN + "ping to", address, "OK")
22+
print(Fore.RED, foundagain)
23+
print(Fore.RED, mailsent)
24+
print(Style.RESET_ALL)
25+
if foundagain == True and mailsent == False:
26+
email()
27+
mailsent = True
28+
print(Fore.GREEN + "-----MAIL GESENDET-----")
29+
print(Style.RESET_ALL)
30+
31+
if foundagain == False and mailsent == False:
32+
foundagain = True
33+
34+
if foundagain == False and mailsent == True:
35+
foundagain = True
36+
mailsent = False
37+
38+
39+
40+
elif res == 2:
41+
print ("no response from", address)
42+
foundagain = False
43+
else:
44+
print (Fore.YELLOW + "ping to", address, "failed!")
45+
print(Style.RESET_ALL)
46+
print(Fore.RED, foundagain)
47+
print(Fore.RED, mailsent)
48+
print(Style.RESET_ALL)
49+
failed += 1
50+
print(failed)
51+
if failed >= 3:
52+
failed = 0
53+
foundagain = False
54+
mailsent = False
55+
threading.Timer(2, sniff).start()
56+
57+
def email():
58+
import smtplib
59+
from socket import gaierror
60+
from email.mime.text import MIMEText as text
61+
from datetime import datetime
62+
63+
now = datetime.now()
64+
current_time = now.strftime("%H:%M:%S")
65+
66+
67+
# Now you can play with your code. Let’s define the SMTP server separately here:
68+
port = 587
69+
smtp_server = config['SMTP']['SERVER']
70+
sender = config['SMTP']['EMAIL']
71+
password = config['SMTP']['PASSWORD']
72+
# Specify the sender’s and receiver’s email addresses:
73+
receiver = config['SMTP']['RECEIVER']
74+
75+
# type your message: use two newlines (\n) to separate the subject from the message body, and use 'f' to automatically insert variables in the text
76+
message="Miris Handy hat sich um "+ current_time+ " mit dem WLAN verbunden"
77+
m = text(message)
78+
79+
m['Subject'] = config['SMTP']['SUBJECT']
80+
m['From'] = sender
81+
m['To'] = receiver
82+
83+
84+
try:
85+
# Send your message with credentials specified above
86+
server = smtplib.SMTP(smtp_server, port)
87+
server.connect(smtp_server,port)
88+
server.ehlo()
89+
server.starttls()
90+
server.ehlo()
91+
server.login(sender, password)
92+
server.sendmail(sender, receiver, m.as_string())
93+
server.quit()
94+
except (gaierror, ConnectionRefusedError):
95+
# tell the script to report if your message was sent or which errors need to be fixed
96+
print('Failed to connect to the server. Bad connection settings?')
97+
except smtplib.SMTPServerDisconnected:
98+
print('Failed to connect to the server. Wrong user/password?')
99+
except smtplib.SMTPException as e:
100+
print('SMTP error occurred: ' + str(e))
101+
else:
102+
print('Sent')
103+
104+
sniff()

0 commit comments

Comments
 (0)