Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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")
Add Comment
Please, Sign In to add comment