-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
executable file
·60 lines (49 loc) · 1.53 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/usr/bin/env python
# nasacct/monitor-ip
# containerized app to register in a log IP changes from routers.
# implemented as a dynamic IP service this app will record IPs set with
# http://server:port/update/<ip>
# and will return the last logged IP with:
# http://server:port/ip
#
# programmed by n4s (@github)
from flask import *
import os
import datetime
import re
application = Flask(__name__)
port=os.getenv("PORT")
@application.route("/")
def hello():
return "<h1>Update Monitor App</h1><p>This is an update IP docker container that logs /update/<IP> GET requests to a log file</p>"
# update ip path
@application.route("/update/<ip>")
def log(ip):
application.logger.debug("Logging IP [%s]\n" % ip)
f = open("/log/monitor-ip.log","a")
f.write("%s: %s\n" % (datetime.datetime.now().strftime("%Y-%m-%d %H:%M"),ip))
f.close()
return "OK\n"
# get ip path
@application.route("/ip")
def getip():
# open file, go to end, look for last "\n"
try:
f = open("/log/monitor-ip.log")
f.seek(0, 2)
while f.tell() > 0:
f.seek(-2,1)
c = f.read(1)
if c == '\n':
break
line = f.readline().strip()
f.close()
# get IP
m = re.search("[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$", line)
ip = m.group(0)
application.logger.debug("Returning IP [%s]\n" % ip)
return "%s\n" % ip
except:
return "ERROR - can not open log file\n"
if __name__ == "__main__":
application.run(host='0.0.0.0',port=port)