-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathipwatch.py
More file actions
53 lines (45 loc) · 1.47 KB
/
ipwatch.py
File metadata and controls
53 lines (45 loc) · 1.47 KB
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
import requests
from tinydb import TinyDB, Query
from tinydb.operations import increment as dbinc, set as dbset
import datetime
import envConfig
import notificationemail
env_config = envConfig.load_config()
try:
result = requests.get(env_config['end_point'])
except Exception as err:
# TODO: log this
print(f'logging {err}')
if result.status_code == 200:
jresult = result.json();
cur_ip = jresult['x-forwarded-for']
if len(cur_ip) > 0:
db = TinyDB('./data.json')
Log = Query()
now = datetime.datetime.now(datetime.UTC).isoformat()
row = db.get(Log.ip == cur_ip)
if row is not None:
print(f'found ip {row['ip']}, seen {row['seen']} times');
# couldn't figure out how to do mulple operations on one doc
# in a single call, e.g. using `update_multiple()`
db.update(dbinc('seen'), doc_ids=[row.doc_id])
db.update(dbset('timestamp', now), doc_ids=[row.doc_id])
else:
# new ip
print('new ip discovered', cur_ip)
subject='[Action Required] New IP discoved from our ISP'
body='\n\n**DNS NOT updated**. Updates are disabled or not configured. The new IP will need to be updated manually.'
body += '\n\nIP: ' + cur_ip
notificationemail.send(subject, body)
db.insert({
'ip': cur_ip,
'seen': 1,
'timestamp': datetime.datetime.now(datetime.UTC).isoformat(),
})
else:
# send error email (no ip in x-forwarded-for)
pass
else:
# record issue
pass
print('end')