Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Mike Stephanick
- #Week 7 Final Submission
- import smtplib
- import socket
- import datetime
- import sqlite3
- import os
- DIR_NAME = os.path.dirname(__file__)
- path = os.path.join(DIR_NAME, 'con.db') #Defines the path and idrectory for the db file
- current_time = datetime.datetime.now().time()
- # lines 9-13 specify which socket forms to use and the host and port
- s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- host = '192.168.0.29'
- port = 80
- #sql_main connects to the db, creates a new table and rows, then inserts data from those sources into those colums
- def sql_main():
- global path
- sqlConn = sqlite3.connect(path)
- sqlConn.execute('CREATE TABLE IF NOT EXISTS Http(Date TEXT, Status TEXT)') # Creates the new table and colums if they dont already exist
- 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
- #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
- def sendMail():
- mailApp = smtplib.SMTP('smtp.gmail.com', 587)
- content = "Host is down"
- mailApp.ehlo()
- mailApp.starttls()
- mailApp.login('pythonalerts4242@gmail.com', 'donthackmepls')
- mailApp.sendmail('yougay@gmail.com', 'yougay@gmail.com', content)
- #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
- def conn_host():
- global status
- status = 0
- while 1:
- try:
- s.connect((host, port))
- status = 1
- s.close
- except:
- status
- NameError
- finally:
- if status == 0:
- print('down')
- sendMail()
- sql_main()
- break
- conn_host()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement