SHOW:
|
|
- or go back to the newest paste.
| 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 | - | while (not success or tries > 5): |
| 18 | + | |
| 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 | - | print "tries: %d, sleeping for 10 secs" % tries |
| 24 | + | |
| 25 | tries += 1 | |
| 26 | logf.write("tries: %d, sleeping for 10 secs\n" % tries)
| |
| 27 | time.sleep(10) | |
| 28 | - | return fqn, ext_ip |
| 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 | - | fqn, ext_ip = check_in() |
| 56 | + | |
| 57 | ||
| 58 | - | print "FQN: %s" % fqn |
| 58 | + | logf = open('updateip.log', 'a')
|
| 59 | - | print "Ext IP: %s" % ext_ip |
| 59 | + | tstamp = datetime.now() |
| 60 | - | filedata = "" |
| 60 | + | logf.write("updateip.py starting at: %s\n" % tstamp)
|
| 61 | - | if (os.path.exists('ipaddr.txt')):
|
| 61 | + | success, fqn, ext_ip = check_in() |
| 62 | - | f = open('ipaddr.txt', 'r')
|
| 62 | + | |
| 63 | - | filedata = f.read() |
| 63 | + | if (not success): |
| 64 | - | f.close() |
| 64 | + | logf.write("Couldn't get current ip, will try again in 20 mins...\n")
|
| 65 | - | if (str(filedata) == ext_ip): |
| 65 | + | |
| 66 | - | print "ip match!" |
| 66 | + | logf.write("FQN: %s\n" % fqn)
|
| 67 | logf.write("Ext IP: %s\n" % ext_ip)
| |
| 68 | - | print "IPs don't match, updating ipaddr.txt..." |
| 68 | + | filedata = "" |
| 69 | - | f = open('ipaddr.txt', 'w')
|
| 69 | + | if (os.path.exists('ipaddr.txt')):
|
| 70 | - | f.write(ext_ip) |
| 70 | + | f = open('ipaddr.txt', 'r')
|
| 71 | - | f.close() |
| 71 | + | filedata = f.read() |
| 72 | - | print "...sending mail..." |
| 72 | + | f.close() |
| 73 | - | sendMail(['Jules <[email protected]>'],'YoungJules <[email protected]>','Hello from your server!','FQN: %s, IP address: %s' % (fqn, ext_ip),[]) |
| 73 | + | if (str(filedata) == ext_ip): |
| 74 | - | print "...all done!" |
| 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() |