Created
July 8, 2014 01:17
-
-
Save itsbalamurali/4ba1f432f59e71583492 to your computer and use it in GitHub Desktop.
Python script for detecting IP change and send email to admin.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import os.path | |
| import time | |
| import urllib | |
| import re | |
| from email.MIMEMultipart import MIMEMultipart | |
| from email.MIMEText import MIMEText | |
| import smtplib | |
| fileName = "mylastip.txt" | |
| fileExist = os.path.isfile(fileName) | |
| if fileExist : | |
| fp = open (fileName, "r") | |
| last_ip = fp.read() | |
| fp.close() | |
| else : | |
| last_ip = "0.0.0.0" | |
| url = "http://checkip.dyndns.org " | |
| while (True): | |
| request = urllib.urlopen(url).read() | |
| theIP = re.findall(r"\d{1,3}\.\d{1,3}\.\d{1,3}.\d{1,3}", request) | |
| theIP2 = theIP[0] | |
| if ( theIP2 != last_ip ): | |
| fp = open (fileName, "w") | |
| fp.write(theIP2) | |
| fp.close() | |
| fromaddr = "<myname>@gmail.com" | |
| toaddr = "<myname>@gmail.com" | |
| msg = MIMEMultipart() | |
| msg['From'] = fromaddr | |
| msg['To'] = toaddr | |
| msg['Subject'] = "my server ip address changed" | |
| body = theIP2 | |
| msg.attach(MIMEText(body, 'plain')) | |
| server = smtplib.SMTP('smtp.gmail.com', 587) | |
| server.ehlo() | |
| server.starttls() | |
| server.ehlo() | |
| server.login("<myname>", "<mypassword>") | |
| text = msg.as_string() | |
| server.sendmail(fromaddr, toaddr, text) | |
| server.quit() | |
| last_ip = theIP2 | |
| time.sleep(120) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment