YoungJules

PyDynDns2

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