Advertisement
Guest User

nikka

a guest
Nov 12th, 2017
586
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.07 KB | None | 0 0
  1. #Mike Stephanick
  2. #Week 7 Final Submission
  3.  
  4.  
  5. import smtplib
  6. import socket
  7. import datetime
  8. import sqlite3
  9. import os
  10. DIR_NAME = os.path.dirname(__file__)
  11. path = os.path.join(DIR_NAME, 'con.db') #Defines the path and idrectory for the db file
  12.  
  13. current_time = datetime.datetime.now().time()
  14. # lines 9-13 specify which socket forms to use and the host and port
  15. s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  16. host = '192.168.0.29'
  17. port = 80
  18.  
  19. #sql_main connects to the db, creates a new table and rows, then inserts data from those sources into those colums
  20. def sql_main():
  21. global path
  22.  
  23.  
  24. sqlConn = sqlite3.connect(path)
  25.  
  26.  
  27. sqlConn.execute('CREATE TABLE IF NOT EXISTS Http(Date TEXT, Status TEXT)') # Creates the new table and colums if they dont already exist
  28. sqlConn.execute("INSERT INTO HTTP (Date, Status) values (?, ?)", (str(current_time), str(status))) # turns date and colum data into a string input to the SQL table
  29.  
  30. #sendMail is a function that sends emails, fitst we identify the method with the host and port, then we start the encryption layer(TLS) with the smtp server, then we send mail from python alerts to mastermike1257
  31. def sendMail():
  32. mailApp = smtplib.SMTP('smtp.gmail.com', 587)
  33. content = "Host is down"
  34. mailApp.ehlo()
  35. mailApp.starttls()
  36. mailApp.login('pythonalerts4242@gmail.com', 'donthackmepls')
  37. mailApp.sendmail('yougay@gmail.com', 'yougay@gmail.com', content)
  38.  
  39. #conn_host connects to the host listed above and is tried, it it is good, status =1 if it cant connect status =0 then it calls to sendmail to send mail if its down, then logs action in DB, finally breaking the while loop
  40. def conn_host():
  41. global status
  42. status = 0
  43. while 1:
  44. try:
  45.  
  46. s.connect((host, port))
  47. status = 1
  48. s.close
  49.  
  50.  
  51.  
  52. except:
  53. status
  54. NameError
  55. finally:
  56. if status == 0:
  57. print('down')
  58. sendMail()
  59. sql_main()
  60.  
  61. break
  62.  
  63.  
  64.  
  65.  
  66. conn_host()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement