Created
January 5, 2017 04:31
-
-
Save iacchus/8874c919de2b3dbdd677abe9e4126042 to your computer and use it in GitHub Desktop.
Simple log watcher in python3 which sends GET to / to PUSHOVER
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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) |
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
This is for me only, if you need something like this ask me because I did not published iplib yet.