YoungJules

PyIpWatch

Jan 5th, 2012
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.42 KB | None | 0 0
  1. #! /usr/bin/python
  2.  
  3. import os
  4. import time
  5. import urllib2
  6. import smtplib
  7. from email.MIMEMultipart import MIMEMultipart
  8. from email.MIMEBase import MIMEBase
  9. from email.MIMEText import MIMEText
  10. from email.Utils import COMMASPACE, formatdate
  11. from email import Encoders
  12. from datetime import datetime
  13.  
  14. def check_in():
  15.  
  16.     fqn = os.uname()[1]
  17.     tries = 0
  18.     success = False
  19.     ext_ip = ""
  20.     while (success == False and tries < 5):
  21.         try:
  22.             ext_ip = urllib2.urlopen('http://automation.whatismyip.com/n09230945.asp').read()
  23.             success = True
  24.         except:
  25.             tries += 1
  26.             logf.write("tries: %d, sleeping for 20 secs\n" % tries)
  27.             time.sleep(20)
  28.                
  29.     return success, fqn, ext_ip
  30.  
  31. def sendMail(to, fro, subject, text, files=[],server="localhost"):
  32.     assert type(to)==list
  33.     assert type(files)==list
  34.  
  35.  
  36.     msg = MIMEMultipart()
  37.     msg['From'] = fro
  38.     msg['To'] = COMMASPACE.join(to)
  39.     msg['Date'] = formatdate(localtime=True)
  40.     msg['Subject'] = subject
  41.  
  42.     msg.attach( MIMEText(text) )
  43.  
  44.     for file in files:
  45.         part = MIMEBase('application', "octet-stream")
  46.         part.set_payload( open(file,"rb").read() )
  47.         Encoders.encode_base64(part)
  48.         part.add_header('Content-Disposition', 'attachment; filename="%s"'
  49.                        % os.path.basename(file))
  50.         msg.attach(part)
  51.  
  52.     smtp = smtplib.SMTP(server)
  53.     smtp.sendmail(fro, to, msg.as_string() )
  54.     smtp.close()
  55.  
  56.  
  57. logf = open('updateip.log', 'a')
  58. tstamp = datetime.now()
  59. logf.write("updateip.py starting at: %s\n" % tstamp)
  60. success, fqn, ext_ip = check_in()
  61.  
  62. if (not success):
  63.     logf.write("Couldn't get current ip, will try again in 20 mins...\n")
  64. else:
  65.     logf.write("FQN: %s\n" % fqn)
  66.     logf.write("Ext IP: %s\n" % ext_ip)
  67.     filedata = ""
  68.     if (os.path.exists('ipaddr.txt')):
  69.         f = open('ipaddr.txt', 'r')
  70.         filedata = f.read()
  71.         f.close()
  72.     if (str(filedata) == ext_ip):
  73.         logf.write("IP address matches!\n")
  74.     else:
  75.         logf.write("IPs don't match, updating ipaddr.txt...\n")
  76.         f = open('ipaddr.txt', 'w')
  77.         f.write(ext_ip)
  78.         f.close()
  79.         logf.write("...sending mail...\n")
  80.         sendMail(['To <[email protected]>'],'From <[email protected]>','Hello from %s!' % fqn,'Server external IP address: %s' % ext_ip,[])
  81.         logf.write("...all done!\n")
  82. logf.close()
Add Comment
Please, Sign In to add comment