Skip to content

Instantly share code, notes, and snippets.

@iacchus
Created January 5, 2017 04:31
Show Gist options
  • Save iacchus/8874c919de2b3dbdd677abe9e4126042 to your computer and use it in GitHub Desktop.
Save iacchus/8874c919de2b3dbdd677abe9e4126042 to your computer and use it in GitHub Desktop.
Simple log watcher in python3 which sends GET to / to PUSHOVER
#!/usr/bin/env python3
# follow.py
#
# Follow a file like tail -f.
import sys
import time
from iplib import check_ip as CHECKIP
import requests
import http.client, urllib
def send_pushover(msg):
data = {
'token' : 'APP_TOKEN',
'user' : 'USER_TOKEN',
'message' : msg
}
r = requests.post("https://api.pushover.net/1/messages.json", data=data)
return r
def follow(thefile):
thefile.seek(0,2)
while True:
line = thefile.readline()
if not line:
time.sleep(0.5)
continue
yield line
if __name__ == '__main__':
logfilename = sys.argv[1]
print("Log filename is: {}".format(logfilename))
logfile = open(logfilename, "r")
loglines = follow(logfile)
for line in loglines:
if "GET / " in line:
ip = line.split(' ',1)[0]
if CHECKIP(ip):
print(line,)
send_pushover(line)
@iacchus
Copy link
Author

iacchus commented Jan 5, 2017

This is for me only, if you need something like this ask me because I did not published iplib yet.

@cookandy
Copy link

I could benefit from something this if you feel like sharing iplib

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment