Guest User

Untitled

a guest
Dec 24th, 2018
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.09 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 = 'send 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('smtp server', 587)
  23. smtpserver.starttls()
  24.  
  25. # Perform operations via server
  26. server.login('smtp server login', 'password')
  27. server.sendmail(sender, [recipient], msg.as_string())
  28. server.quit()
  29.  
  30. host = ('ip', 'ip', 'ip')
  31. user = 'switch admin'
  32. secret = 'switch pass'
  33. port = 22
  34.  
  35. remote_conn_pre = paramiko.SSHClient()
  36.  
  37. remote_conn_pre.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  38.  
  39. # connect for each host and check temperature
  40. for switch in host:
  41. remote_conn_pre.connect(switch, username=user, password=secret, look_for_keys=False, allow_agent=False)
  42. print("--------------------------------------------")
  43. print("SSH connection established to " + switch)
  44.  
  45. remote_conn = remote_conn_pre.invoke_shell()
  46. print("Interactive SSH session established")
  47.  
  48. output = remote_conn.recv(1000)
  49.  
  50. remote_conn.send("\n")
  51. remote_conn.send("show system temperature\n") # Command to check temperature on switch
  52.  
  53. time.sleep(3)
  54.  
  55. output = remote_conn.recv(5000)
  56.  
  57. string = str(output).split()[42] # split output for easy extraction of temperature value
  58. integer = int(string[:len(string)-1]) # temperature value
  59.  
  60. print(integer)
  61.  
  62. if integer > 47:
  63. print("The temperature on switch %s reached %dC!!" %(switch, integer))
  64. SendMail("The temperature on switch %s reached %dC!!\n"
  65. "Date: %s" %(switch, integer, currentDate))
  66. else:
  67. print("The temperature on switch %s is OK\n"
  68. "Date: %s" %(switch, currentDate))
  69. print("--------------------------------------------\n")
Add Comment
Please, Sign In to add comment