Guest User

Untitled

a guest
Mar 15th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.12 KB | None | 0 0
  1. import paramiko
  2. import smtplib
  3. from email.mime.text import MIMEText
  4. import time, datetime
  5.  
  6. date = datetime.datetime.now()
  7. currentDate = date.strftime("%d %B %Y %I:%M%p")
  8.  
  9. # Mail sender function
  10. def SendMail(alert):
  11. # Define to/from
  12. sender = 'sender email'
  13. recipient = 'recipient email'
  14.  
  15. # Create message
  16. msg = MIMEText(alert)
  17. msg['Subject'] = "Temperature alarm on switch %s " % switch
  18. msg['From'] = sender
  19. msg['To'] = recipient
  20.  
  21. # Create server object with SSL option
  22. server = smtplib.SMTP_SSL('mail server', 465)
  23.  
  24. # Perform operations via server
  25. server.login('mail login', 'mail password')
  26. server.sendmail(sender, [recipient], msg.as_string())
  27. server.quit()
  28.  
  29. host = ('x.x.x.x', 'x.x.x.x', 'x.x.x.x') # list of IPs of switches
  30. user = 'switch login'
  31. secret = 'switch password'
  32. port = 22
  33.  
  34. remote_conn_pre = paramiko.SSHClient()
  35.  
  36. remote_conn_pre.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  37.  
  38. # connect for each host and check temperature
  39. for switch in host:
  40. remote_conn_pre.connect(switch, username=user, password=secret, look_for_keys=False, allow_agent=False)
  41. print("--------------------------------------------")
  42. print("SSH connection established to " + switch)
  43.  
  44. remote_conn = remote_conn_pre.invoke_shell()
  45. print("Interactive SSH session established")
  46.  
  47. output = remote_conn.recv(1000)
  48.  
  49. remote_conn.send("\n")
  50. remote_conn.send("show system temperature\n") # Command to check temperature on switch
  51.  
  52. time.sleep(3)
  53.  
  54. output = remote_conn.recv(5000)
  55.  
  56. string = str(output).split()[42] # split output for easy extraction of temperature value
  57. integer = int(string[:len(string)-1]) # temperature value
  58.  
  59. print(integer)
  60.  
  61. if integer > 47:
  62. print("The temperature on switch %s reached %dC!!" %(switch, integer))
  63. SendMail("The temperature on switch %s reached %dC!!\n"
  64. "Date: %s" %(switch, integer, currentDate))
  65. else:
  66. print("The temperature on switch %s is OK\n"
  67. "Date: %s" %(switch, currentDate))
  68. print("--------------------------------------------\n")
Add Comment
Please, Sign In to add comment