|  | 
|  | 1 | +#!/bin/python | 
|  | 2 | + | 
|  | 3 | +from bs4 import BeautifulSoup as bs4 | 
|  | 4 | +import requests | 
|  | 5 | +import smtplib | 
|  | 6 | +from email.MIMEMultipart import MIMEMultipart | 
|  | 7 | +from email.MIMEText import MIMEText | 
|  | 8 | + | 
|  | 9 | +fromaddr = "<YOUR GMAIL EMAIL ADDRESS>"  | 
|  | 10 | +toaddr = "<DESIRED EMAIL ADDRESS>" | 
|  | 11 | + | 
|  | 12 | +msg = MIMEMultipart() | 
|  | 13 | + | 
|  | 14 | +# Turn on "Less Secure App for Google accounts. Refer - https://support.google.com/accounts/answer/6010255?hl=en" | 
|  | 15 | + | 
|  | 16 | +msg['From'] = fromaddr | 
|  | 17 | +msg['To'] = toaddr | 
|  | 18 | + | 
|  | 19 | +#Make text file in the same directory for storing current value and avoid repititive emails. | 
|  | 20 | + | 
|  | 21 | +PATH_INPUT = "/<Whatever directory>/current_ip.txt" | 
|  | 22 | + | 
|  | 23 | +get_request = requests.get("http://www.ipvoid.com") | 
|  | 24 | +soup = bs4(get_request.content,'lxml') | 
|  | 25 | +current_ip = soup.find('input', {'name':''})['value'] | 
|  | 26 | +print "Current IP is " + current_ip | 
|  | 27 | + | 
|  | 28 | +with open(PATH_INPUT, "r") as f: | 
|  | 29 | +	existing_IP = f.readlines() | 
|  | 30 | +	for line in existing_IP: | 
|  | 31 | +		existing_IP = str(line).strip('\n') | 
|  | 32 | +	f.close() | 
|  | 33 | + | 
|  | 34 | +if existing_IP == current_ip: | 
|  | 35 | +	print "no email sent!" | 
|  | 36 | + | 
|  | 37 | +else: | 
|  | 38 | +	msg['Subject'] = "Current IP is " + current_ip | 
|  | 39 | +	server = smtplib.SMTP('smtp.gmail.com', 587) | 
|  | 40 | +	server.ehlo() | 
|  | 41 | +	server.starttls() | 
|  | 42 | +	server.ehlo() | 
|  | 43 | +	server.login(fromaddr, '<PASSWORD>') #logins | 
|  | 44 | +	text = msg.as_string() | 
|  | 45 | + | 
|  | 46 | +	server.sendmail(fromaddr, toaddr, text) #sends email | 
|  | 47 | + | 
|  | 48 | +	with open(PATH_INPUT, "w") as f: | 
|  | 49 | +		f.write(str(current_ip + "\n")) | 
|  | 50 | +		f.close() | 
0 commit comments