This repository has been archived by the owner on Jul 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ipwatch.py
executable file
·70 lines (56 loc) · 1.74 KB
/
ipwatch.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
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/python
import logging, os, sys, time
import ConfigParser
import requests
import smtplib
from email.Message import Message
CONFIG_FILE="./ipwatch.ini"
def readConfig():
config = ConfigParser.ConfigParser()
config.read(CONFIG_FILE)
return config
def sendMail(config, value):
notify_email = config.get("notify","email")
smtp_host = config.get("smtp","host")
smtp_port = config.get("smtp","port")
smtp_user = config.get("smtp","user")
smtp_pass = config.get("smtp","pass")
hostname = os.uname()[1]
m = Message()
m['From'] = 'me'
m['bcc'] = notify_email
m['Subject'] = 'IP Address for ' + hostname + ' is now ' + value
server = smtplib.SMTP(smtp_host,smtp_port)
server.starttls()
server.login(smtp_user,smtp_pass)
server.sendmail(smtp_user, notify_email, m.as_string())
server.quit()
def doCheck(config):
check_site = config.get("check","site")
check_cache = config.get("check","cache")
check_delay_s = config.getfloat("check","delay_seconds")
if not os.path.isfile(check_cache):
open(check_cache, 'w').close()
with open (check_cache, "r") as f:
latest_value=f.readline()
while True:
try:
new_value=requests.get(check_site).text
if not new_value == latest_value:
print "New value: " + new_value
sendMail(config, new_value)
with open (check_cache, 'w') as f:
f.write(new_value)
f.close()
latest_value = new_value
except Exception as e:
e = sys.exc_info()
print("Error: {}".format(e))
time.sleep(check_delay_s)
def main():
# set working directory to script path
os.chdir(os.path.dirname(sys.argv[0]))
config = readConfig()
doCheck(config)
if __name__ == "__main__":
main()