-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck_ip.py
More file actions
60 lines (48 loc) · 1.61 KB
/
check_ip.py
File metadata and controls
60 lines (48 loc) · 1.61 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
53
54
55
56
57
58
59
60
from messaging_service import MessagingService
import urllib
import json
import re
import os
# using AWS's simple IP checker since it accepts python's default user agent header
CHECK_IP_URL = 'http://checkip.amazonaws.com'
# save file path
SAVE_FILE = './ip_save_file.json'
# initiate messaging service
messager = MessagingService('./config.json', "check_ip")
# get old IP address (if any)
old_ip = None
if os.path.isfile(SAVE_FILE):
try:
# open and parse old IP file
with open(SAVE_FILE) as save_file:
save = json.load(save_file)
old_ip = save.get('ip')
except:
# send error
messager.send_error("Unable to open and parse IP save file.")
external_ip = None
try:
# get response
response = urllib.request.urlopen(CHECK_IP_URL).read()
# parse ip from response
external_ip = response.decode('utf8').rstrip()
except:
#send error
messager.send_error("Finding and parsing of external IP address failed.")
# pattern to match IP addresses
ip_pattern = re.compile(r'^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$')
if external_ip is not None and ip_pattern.match(external_ip):
# check if IP is new
if old_ip is None or old_ip != external_ip:
# send IP
messager.send("New External IP detected: " + external_ip)
try:
# write file
with open(SAVE_FILE, 'w') as save_file:
save_file.write('{"ip":"' + external_ip + '"}')
except:
# send error
messager.send_error("Failed to write new IP save file.")
else:
# send error
messager.send_error("Parsed a malformed IP.")