forked from boxcontrol/python_report
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython_report.py
More file actions
48 lines (37 loc) · 1.46 KB
/
python_report.py
File metadata and controls
48 lines (37 loc) · 1.46 KB
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
#!/usr/local/bin/python
import commands
import smtplib, os, sys
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email.MIMEText import MIMEText
from email.Utils import COMMASPACE, formatdate
from email import Encoders
logfile = open('/pyth/to/message.txt', 'w')
logfile.truncate()
#logfile.close()
comm_list = ['uptime', 'vnstat -d', 'free -m', 'df -h', "cat /var/log/auth.log | grep 'sshd.*Invalid'", "cat /var/log/auth.log | grep 'sshd.*opened'"]
for i in comm_list:
stat, out = commands.getstatusoutput(i)
if not stat:
logfile.write(out)
logfile.close()
#send email
def send_mail(send_from, send_to, subject, text, files=[], server="localhost"):
assert type(send_to)==list
assert type(files)==list
msg = MIMEMultipart()
msg['From'] = send_from
msg['To'] = COMMASPACE.join(send_to)
msg['Date'] = formatdate(localtime=True)
msg['Subject'] = subject
msg.attach( MIMEText(text) )
for f in files:
part = MIMEBase('application', "octet-stream")
part.set_payload( open(f,"rb").read() )
Encoders.encode_base64(part)
part.add_header('Content-Disposition', 'attachment; filename="%s"' % os.path.basename(f))
msg.attach(part)
smtp = smtplib.SMTP(server)
smtp.sendmail(send_from, send_to, msg.as_string())
smtp.close()
send_mail('root@example.com', ['personal@mail.com'], 'Daily Server Report', 'Your Daily Server Report:', ['/path/to/message.txt'])