forked from extremenetworks/ExtremeScripting
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathemail.py
More file actions
79 lines (67 loc) · 3.94 KB
/
email.py
File metadata and controls
79 lines (67 loc) · 3.94 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
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
import smtplib
import sys
import re
import datetime
from email.mime.text import MIMEText
def sendemail(txt,me,to,smtp):
msg = {}
msg['From'] = me
s = smtplib.SMTP(smtp)
s.sendmail(me, [to], txt)
s.quit()
def switchData():
data = exsh.clicmd("show switch",capture=True)
switch = {}
for line in data.splitlines():
m = re.search(r'SysName:\s+(\S+)',line)
if m:
switch['Name'] = m.group(1)
return switch
def LogLines(minutes=10):
now = datetime.datetime.now()
logstart = now - datetime.timedelta(minutes=minutes)
cmd = "show log starting time " + logstart.strftime('%H:%M:%S')
log = exsh.clicmd(cmd,capture=True)
return log
def ChgVR(vr):
try:
f = open('/proc/self/ns_id', 'w')
f.write(vr+'\n')
f.close()
return True
except:
return False
def main():
###################################
# Start User changeable parameters
###################################
# Change below myVirtualRouter to the VR used to reach the smtp server
# 2 Vr-Default
# 0 VR-Mgmt
# Any other number the number of your user created VR.
myVirtualRouter = 2
FromDomain = "user@example.com"
to = 'user@example.com'
smtp = 'smtp.example.com'
###################################
# End User changeable parameters
###################################
if ChgVR(str(myVirtualRouter)):
exsh.clicmd("disable clipaging")
switch = switchData()
msg = "Port Status change on "+switch['Name']+"\n\n"
if sys.argv[2] == "down":
msg += "Port "+sys.argv[1]+" went down"+"\n"
else:
msg += "Port "+sys.argv[1]+" UP, speed "+sys.argv[2]+" "+sys.argv[3]+"\n"
msg += "\nLast 10 minutes log"+"\n"
msg += LogLines(minutes=10)
me = switch['Name']+FromDomain
sendemail(msg,me,to,smtp)
else:
exsh.clicmd("create log message \"Unable to send Email on log event\"")
if __name__ == "__main__":
try:
main()
except SystemExit:
pass