forked from DhanushNehru/Python-Scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_then_notify.py
81 lines (66 loc) · 2.25 KB
/
run_then_notify.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
import subprocess
import sys
import time
import socket
from getpass import getpass
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
user_email = input("Enter your email: ")
user_password = getpass()
smtp_server = "smtp.gmail.com"
smtp_port = 587 # Port 587 for TLS
def send_text_email(subject, message, recipient_email, sender_email, sender_password, smtp_server, smtp_port):
# Create a text/plain message
msg = MIMEMultipart()
msg['From'] = sender_email
msg['To'] = recipient_email
msg['Subject'] = subject
# Attach the text message
msg.attach(MIMEText(message, 'plain'))
# Connect to the SMTP server
try:
server = smtplib.SMTP(smtp_server, smtp_port)
server.starttls()
server.login(sender_email, sender_password)
text = msg.as_string()
server.sendmail(sender_email, recipient_email, text)
server.quit()
except Exception as e:
print(f"Email could not be sent: {str(e)}")
def run_then_notify(command):
try:
start_time = time.time()
completed_process = subprocess.run(
command,
text=True,
shell=True,
stdout=sys.stdout,
stderr=subprocess.STDOUT,
stdin=sys.stdin
)
end_time = time.time()
execution_time = end_time - start_time
notif_subject = '[Run completed] Your command finished execution'
notif_message = f"Command '{command}' completed\n\
\twith exit code {completed_process.returncode}\n\
\tin {execution_time:.2f} seconds\n\
\n\
This is an automated message sent from your device {socket.gethostname()}"
send_text_email(
subject=notif_subject,
message=notif_message,
recipient_email=user_email,
sender_email=user_email,
sender_password=user_password,
smtp_server=smtp_server,
smtp_port=smtp_port
)
except Exception as e:
print(f"Error: {e}")
if __name__ == "__main__":
if len(sys.argv) != 2:
print("Usage: python run_then_notify.py 'your_command_here'")
sys.exit(1)
command = sys.argv[1]
run_then_notify(command)