-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpingTester.py
141 lines (118 loc) · 4.39 KB
/
pingTester.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import os
import sys
import getopt
import platform
import email.message
import smtplib
from slack_sdk import WebClient
hostList = {"Device 01": "192.168.8.100",
"Device 02": "192.168.8.101"}
numOfTests = "1"
to_name = "Receiver_Name"
to_email = "Receiver_Email"
cc_email = ['CC_email_1', 'CC_email_2']
slack_token = "bot-token"
slack_channel = '#channel-name'
def connectionTest(numOfTests, hostList):
found_list = []
for key, value in hostList.items():
pingResult = ping(numOfTests, value)
if not pingResult:
found_list.append((key, value))
return found_list
def ping(numOfTests, host):
res = False
ping_param = "-n " if platform.system().lower() == "windows" else "-c "
ttl = "TTL=" if platform.system().lower() == "windows" else "ttl="
resultado = os.popen("ping " + ping_param +
numOfTests + " " + host).read()
if ttl in resultado:
res = True
return res
def slack_msg_send(slack_token, slack_channel, msg):
client = WebClient(token=slack_token)
client.chat_postMessage(channel=slack_channel, blocks=msg)
def slack_msg_create(result):
temp_msg = ""
for i in result:
temp_msg = temp_msg + ":electric_plug: {} ({}) \n".format(i[0], i[1])
msg = [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": 'Warning! unreachable device/s detected. :mega: \n' + temp_msg
}
}
]
return msg
def email_send(name, email_addr, username, password, server, port, emailfrom, msg, cc_email):
try:
email_content = msg
email_server = server + ": " + port
msg = email.message.Message()
msg['Subject'] = 'Warning! unreachable device/s detected'
msg['From'] = emailfrom
msg['To'] = email_addr
msg['Cc'] = ', '.join(cc_email)
msg.add_header('Content-Type', 'text/html')
msg.set_payload(email_content)
smtp = smtplib.SMTP(email_server)
smtp.starttls()
smtp.login(username, password)
smtp.send_message(msg)
print("email sent to: {} - {}".format(name, email_addr))
except:
print("Failed to send the email. Please check the email server credentials.")
def msg_create(result, to_name):
msg = """ <h3>Hi {},</h3>
The following device/s are unreachable at the moment. Please take the necessary actions.<br><br>
Device List:<br>""".format(to_name)
msg = msg + "<ul>"
for i in result:
msg = msg + "<li><b>{} ({})</b></li>".format(i[0], i[1])
msg = msg + "</ul>"
msg = msg + "<br><hr><i>This is an auto-generated email by pingTester.</i><hr>"
return msg
def user_arg(argv):
arg_emailuser = ""
arg_emailpass = ""
arg_emailserv = ""
arg_emailport = ""
arg_emailfrom = ""
arg_help = "{0} -u <emailuser> -p <emailpass> -s <emailserv> -o <emailport> -f <emailfrom>".format(
argv[0])
try:
opts, args = getopt.getopt(argv[1:], "hu:p:s:o:f:", [
"help", "emailuser=", "emailpass=", "emailserv=", "emailport=", "emailfrom="])
except:
print(arg_help)
sys.exit(2)
for opt, arg in opts:
if opt in ("-h", "--help"):
print(arg_help)
sys.exit(2)
elif opt in ("-u", "--emailuser"):
arg_emailuser = arg
elif opt in ("-p", "--emailpass"):
arg_emailpass = arg
elif opt in ("-s", "--emailserv"):
arg_emailserv = arg
elif opt in ("-o", "--emailport"):
arg_emailport = arg
elif opt in ("-f", "--emailfrom"):
arg_emailfrom = arg
return arg_emailuser, arg_emailpass, arg_emailserv, arg_emailport, arg_emailfrom
def main(to_name, to_email, cc_email, numOfTests, hostList, slack_token, slack_channel):
if __name__ == "__main__":
emailuser, emailpass, emailserv, emailport, emailfrom = user_arg(
sys.argv)
result = connectionTest(numOfTests, hostList)
if result != []:
email_msg = msg_create(result, to_name)
slack_msg = slack_msg_create(result)
email_send(to_name, to_email, emailuser, emailpass,
emailserv, emailport, emailfrom, email_msg, cc_email)
slack_msg_send(slack_token, slack_channel, slack_msg)
main(to_name, to_email, cc_email, numOfTests,
hostList, slack_token, slack_channel)