
GetExtIp
By: a guest on
Aug 11th, 2012 | syntax:
None | size: 0.81 KB | hits: 9 | expires: Never
from urllib2 import urlopen
import cStringIO
import sys
import smtplib
import os
#Get External IP
ip = urlopen('http://www.icanhazip.com').read()
#Check if extip.txt exists
if os.access('/home/sublim3/Coding/Python/extip', os.F_OK):
print "Good"
else:
f = open("extip.txt", 'w')
f.close()
#Compare External IP with File
f = open("extip.txt", 'r')
text = f.readline()
if text == ip:
f.close()
exit()
else:
with open('extip.txt', 'w') as f:
f.write(ip)
##Send mail
fromaddr = 'example@example.com'
toaddr = 'example@example.com'
msg = ip
#Credentials
username = 'user'
password = 'pass'
#Mail Send
server = smtplib.SMTP('smtp.gmail.com:587')
server.starttls()
server.login(username,password)
server.sendmail(fromaddr,toaddr,msg)
server.quit()
f.close()