Skip to content

Instantly share code, notes, and snippets.

@jamil666
Created December 24, 2018 13:26
Show Gist options
  • Save jamil666/dbb9ec6be4f051b53439f2a5e0cb1123 to your computer and use it in GitHub Desktop.
Save jamil666/dbb9ec6be4f051b53439f2a5e0cb1123 to your computer and use it in GitHub Desktop.
Switches temperature monitoring (HP Procurve)
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 = 'send 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('smtp server', 587)
smtpserver.starttls()
# Perform operations via server
server.login('smtp server login', 'password')
server.sendmail(sender, [recipient], msg.as_string())
server.quit()
host = ('ip', 'ip', 'ip')
user = 'switch admin'
secret = 'switch pass'
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