Created
March 6, 2018 06:56
-
-
Save jamil666/62a5996fd80a27a496cb244002ab2acd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import paramiko | |
import smtplib | |
from email.mime.text import MIMEText | |
import time, datetime | |
date = datetime.datetime.now() | |
currentDate = date.strftime("%d %B %Y %I:%M%p") | |
# Mail sender function | |
def SendMail(alert): | |
# Define to/from | |
sender = 'sender email' | |
recipient = 'recipient email' | |
# Create message | |
msg = MIMEText(alert) | |
msg['Subject'] = "Temperature alarm on switch %s " % switch | |
msg['From'] = sender | |
msg['To'] = recipient | |
# Create server object with SSL option | |
server = smtplib.SMTP_SSL('mail server', 465) | |
# Perform operations via server | |
server.login('mail login', 'mail password') | |
server.sendmail(sender, [recipient], msg.as_string()) | |
server.quit() | |
host = ('x.x.x.x', 'x.x.x.x', 'x.x.x.x') # list of IPs of switches | |
user = 'switch login' | |
secret = 'switch password' | |
port = 22 | |
remote_conn_pre = paramiko.SSHClient() | |
remote_conn_pre.set_missing_host_key_policy(paramiko.AutoAddPolicy()) | |
# connect for each host and check temperature | |
for switch in host: | |
remote_conn_pre.connect(switch, username=user, password=secret, look_for_keys=False, allow_agent=False) | |
print("--------------------------------------------") | |
print("SSH connection established to " + switch) | |
remote_conn = remote_conn_pre.invoke_shell() | |
print("Interactive SSH session established") | |
output = remote_conn.recv(1000) | |
remote_conn.send("\n") | |
remote_conn.send("show system temperature\n") # Command to check temperature on switch | |
time.sleep(3) | |
output = remote_conn.recv(5000) | |
string = str(output).split()[42] # split output for easy extraction of temperature value | |
integer = int(string[:len(string)-1]) # temperature value | |
print(integer) | |
if integer > 47: | |
print("The temperature on switch %s reached %dC!!" %(switch, integer)) | |
SendMail("The temperature on switch %s reached %dC!!\n" | |
"Date: %s" %(switch, integer, currentDate)) | |
else: | |
print("The temperature on switch %s is OK\n" | |
"Date: %s" %(switch, currentDate)) | |
print("--------------------------------------------\n") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment