Don't like ads? PRO users don't see any ads ;-)
Guest

GetExtIp

By: a guest on Aug 11th, 2012  |  syntax: None  |  size: 0.81 KB  |  hits: 9  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. from urllib2 import urlopen
  2. import cStringIO
  3. import sys
  4. import smtplib
  5. import os
  6.  
  7. #Get External IP
  8.  
  9. ip = urlopen('http://www.icanhazip.com').read()
  10.  
  11. #Check if extip.txt exists
  12.  
  13. if os.access('/home/sublim3/Coding/Python/extip', os.F_OK):
  14.         print "Good"
  15. else:
  16.         f = open("extip.txt", 'w')
  17.         f.close()
  18.  
  19. #Compare External IP with File
  20.  
  21. f = open("extip.txt", 'r')
  22. text = f.readline()
  23. if text == ip:
  24.         f.close()
  25.         exit()
  26. else:
  27.         with open('extip.txt', 'w') as f:
  28.                 f.write(ip)
  29.                
  30.         ##Send mail
  31.        
  32.         fromaddr = 'example@example.com'
  33.         toaddr = 'example@example.com'
  34.         msg = ip
  35.  
  36.         #Credentials
  37.         username = 'user'
  38.         password = 'pass'
  39.  
  40.         #Mail Send
  41.         server = smtplib.SMTP('smtp.gmail.com:587')
  42.         server.starttls()
  43.         server.login(username,password)
  44.         server.sendmail(fromaddr,toaddr,msg)
  45.         server.quit()
  46.         f.close()