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
Latest commit
48 lines (37 loc) · 1.46 KB
/
Copy pathpython_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
importcommands
importsmtplib, os, sys
fromemail.MIMEMultipartimportMIMEMultipart
fromemail.MIMEBaseimportMIMEBase
fromemail.MIMETextimportMIMEText
fromemail.UtilsimportCOMMASPACE, formatdate
fromemailimportEncoders
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'"]
foriincomm_list:
stat, out=commands.getstatusoutput(i)
ifnotstat:
logfile.write(out)
logfile.close()
#send email
defsend_mail(send_from, send_to, subject, text, files=[], server="localhost"):
asserttype(send_to)==list
asserttype(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) )
forfinfiles:
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'])