-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_external_ip_warning.py
51 lines (42 loc) · 1.83 KB
/
check_external_ip_warning.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
__author__ = 'joerod'
#!/usr/bin/env python
import urllib,re,smtplib,email.utils,os.path,datetime
from email.mime.text import MIMEText
#Grabs external ip from site
file_path = '/Users/joerod/Desktop/ip.txt'
d = datetime.datetime.now()
def get_external_ip():
site = urllib.urlopen("http://checkip.dyndns.org/").read()
grab = re.findall('([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)', site)
address = grab[0]
return address
if __name__ == '__main__':
#checks file for saved IP address to compare with IP from site
#If ip has changed sends email
if get_external_ip().strip() != file.read(open(file_path, 'r')).strip():
print "We gotta problem!"
subject = 'IP has changed for joerod.com %s' %(get_external_ip())
#sends email
text = 'New IP: %s' % (get_external_ip())
msg = MIMEText(text)
msg['To'] = email.utils.formataddr(('Joe Rodriguez', '[email protected]'))
msg['From'] = email.utils.formataddr(('JoeRod.com IP Change', '[email protected]'))
msg['Subject'] = subject
# Credentials (if needed)
username = '[email protected]'
password = ''
# The actual mail send
server = smtplib.SMTP('smtp.gmail.com:587')
server.starttls()
server.login(username,password)
server.sendmail('[email protected]', ['[email protected]'], msg.as_string())
server.quit()
else:
print "All is good! %s" %d.strftime("%m-%d-%Y %H:%M:%S")
#if current time is +1 hours from last write time of file and if IP does not match file, write to file
filetime = os.path.getmtime(file_path)
if filetime > (filetime - 3600) and get_external_ip().strip() != file.read(open(file_path, 'r')).strip():
print "Updating %s with new IP" %file_path
fn = (open(file_path, 'w'))
fn.write(get_external_ip())
fn.close()