-
Notifications
You must be signed in to change notification settings - Fork 4
/
runner.py
71 lines (57 loc) · 2.07 KB
/
runner.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
71
from bs4 import BeautifulSoup
import urllib2
import re
import os
import smtplib
import yaml
from email.mime.text import MIMEText
SERVERNAME = "MyMachine"
EMAIL = "<email>"
PASSWORD = "<password>"
SMTP_SERVER = "smtp.163.com"
SMTP_SERVER_PORT = '25'
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
logFile = os.path.join(BASE_DIR, "autoipchangenotifier.log")
def main():
request = urllib2.urlopen(
'http://www.baidu.com/s?wd=ip&rsv_spt=1&rsv_iqid=0xbefbb8610000747d&issp=1&f=8&rsv_bp=0&rsv_idx=2&ie=utf-8&tn=baiduhome_pg&rsv_enter=1&rsv_sug3=4&rsv_sug1=3&rsv_sug7=100&rsv_sug2=0&inputT=1648&rsv_sug4=1648'
)
soup = BeautifulSoup(request)
ipResponse = soup.find('div', {'class': 'result-op c-container'}).get("fk")
print "Current IP: %s" % ipResponse
ip = re.search('[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+', str(ipResponse)).group(0)
prevIp = ip
try:
with open(logFile, 'r') as log:
prevIp = log.readline()
log.close()
print "Previous IP: %s" % prevIp
except IOError:
print "Fail to get previous ip"
if ip != prevIp:
target_emails = yaml.load(open(
os.path.join(os.path.dirname(__file__), 'emails.yaml')))['emails']
for recipient in target_emails:
msg = MIMEText("New IP address for {servername} is {ip}".format(
servername=SERVERNAME, ip=ip
))
msg['Subject'] = 'IP address of {servername} Changed'.format(
servername=SERVERNAME
)
msg['From'] = EMAIL
print "Connecting to smtp server"
server = smtplib.SMTP(SMTP_SERVER, SMTP_SERVER_PORT)
server.ehlo()
server.starttls()
server.ehlo()
server.login(EMAIL, PASSWORD)
msg['To'] = recipient
print 'sending email'
server.sendmail(EMAIL, recipient, msg.as_string())
server.quit()
print "Success!"
with open(logFile, 'w') as log:
log.write(ip)
log.close()
if __name__ == "__main__":
main()