Advertisement
Guest User

Untitled

a guest
Dec 5th, 2016
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.06 KB | None | 0 0
  1. # sudo pip install pygtail
  2. # created by hahungkk@gmail.com
  3.  
  4. from pygtail import Pygtail
  5. import sys
  6. import time
  7. import smtplib
  8.  
  9.  
  10. #Gmail only
  11.  
  12. GMAIL_USER = ''
  13. GMAIL_PASSWORD = ''
  14. RECIPIENT = ''
  15. SUBJECT = 'Mail send from tailf log apache'
  16. LOG_PATH = '/home/cherry/sudo.log'
  17. LIST_STRING_TO_PROCESS = ['XXX', 'AAA', 'BBB']
  18.  
  19. def send_email(body):
  20.     FROM = GMAIL_USER
  21.     TO = RECIPIENT if type(RECIPIENT) is list else [RECIPIENT]
  22.     TEXT = body
  23.  
  24.     # Prepare actual message
  25.     message = """From: %s\nTo: %s\nSubject: %s\n\n%s
  26.    """ % (FROM, ", ".join(TO), SUBJECT, TEXT)
  27.     try:
  28.         server = smtplib.SMTP("smtp.gmail.com", 587)
  29.         server.ehlo()
  30.         server.starttls()
  31.         server.login(GMAIL_USER, GMAIL_PASSWORD)
  32.         server.sendmail(FROM, TO, message)
  33.         server.close()
  34.         print('successfully sent the mail')
  35.     except:
  36.         print("failed to send mail")
  37.  
  38.  
  39. while True:
  40.     for line in Pygtail(LOG_PATH):
  41.         if any(x in line for x in LIST_STRING_TO_PROCESS):
  42.             send_email(line)
  43.     time.sleep(0.5)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement