This week only. Pastebin PRO Accounts Christmas Special! Don't miss out!Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Aug 6th, 2010  |  syntax: Python  |  size: 3.64 KB  |  views: 66  |  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. # code by Georgi Talmazov - netskay
  2. # license - open source
  3.  
  4. # main.py
  5.  
  6. from twisted.web.client import getPage
  7. from twisted.internet import reactor, defer, task
  8. import re
  9. import os
  10. import smtp
  11. def content(page):
  12.     inetip=re.findall("\d+\.\d+\.\d+\.\d+", page)
  13.     networkip=os.popen("ipconfig").readlines()
  14.     for line in networkip:
  15.    
  16.         regex=re.findall("[192]+\.[168]+\.\d+\.\d+", line)
  17.         if not regex==[]:
  18.             iplist.append(regex[0])
  19.     iplist.sort()
  20.     p=0
  21.     for i in xrange(1,len(iplist)):
  22.         if iplist[i] != iplist[p]:
  23.             p=p+1
  24.             iplist[p]=iplist[i]
  25.     del iplist[p+1:]
  26.    
  27.     iplistbool=[]
  28.     for ip in prevdict['previplist']:
  29.         if not ip in iplist:
  30.             iplistbool.append(False)
  31.         else: iplistbool.append(True)
  32.    
  33.     if not (inetip[0]==prevdict['previnetip'] or True in iplistbool):
  34.         print '--> IP Change'
  35.         print '::internet: %s'%inetip[0]
  36.         for ip in iplist:
  37.             print '::intranet: %s'%ip
  38.            
  39.         data={}
  40.         mail=smtp.zSMTP()
  41.         data['destination']=['my_email@my_domain.com']
  42.         data['header']='Cube Studios - IP Update'
  43.         data['payload']='New IP assignments on the network.\n\ninternet: %s\nintranet: '%inetip[0]
  44.         for ip in iplist:
  45.             data['payload']=data['payload']+'%s, '%ip
  46.         data['payload']=data['payload'][:-2]
  47.         mail.send(data)
  48.     prevdict['previplist']=iplist
  49.     prevdict['previnetip']=inetip[0]
  50.  
  51.    
  52. def  netError(data):
  53.     print data
  54.  
  55. def  query():
  56.     deferred = getPage('http://ip-adress.com/')
  57.     deferred.addCallback(content)
  58.     deferred.addErrback(netError)
  59.    
  60. print '--> IP Notifier 0.1 by NetSkay - Georgi Talmazov\nInternet\--> Intranet Server IP notification vie email.\n'
  61.  
  62. iplist=[]
  63. prevdict={}
  64. prevdict['previplist']=[]
  65. prevdict['previnetip']=None
  66. lc = task.LoopingCall(query)
  67. lc.start(86400) #86400 seconds in 24 hours
  68. print '--> Initiating reactor service.'
  69. reactor.run()
  70. print '--> Reactor stopped!'
  71.  
  72.  
  73. ############################################################
  74.  
  75. # smtp.py
  76.  
  77. from twisted.internet.threads import deferToThread
  78. from email.mime.multipart import MIMEMultipart, MIMEBase
  79. from email import encoders
  80. import sys, mimetypes, os
  81. import smtplib
  82.  
  83. class zSMTP:
  84.  
  85.     def buildMessage(self,fromaddr, toaddr, subject, body):
  86.         message=MIMEMultipart()
  87.         message['From']=fromaddr
  88.         message['To']=toaddr
  89.         message['Subject']=subject
  90.         textPart=MIMEBase('text', 'plain')
  91.         textPart.set_payload(body)
  92.         message .attach(textPart)
  93.         return message
  94.  
  95.     def sendComplete(self, data, emails):
  96.         #log.zlog.info('Sent log err;to=%s ::-:: %s'%(emails, data))
  97.         print 'email sent to %s' %emails
  98.  
  99.     def send(self, data):
  100.         try:
  101.             mailServer = smtplib.SMTP("smtp.gmail.com", 587)
  102.             mailServer.ehlo()
  103.             mailServer.starttls()
  104.             mailServer.ehlo()
  105.             mailServer.login('mail@mydomain.com', 'mypassword')
  106.             for recvr in data['destination']:
  107.                 msg=self.buildMessage( 'mail@mydomain.com', recvr, data['header'], data['payload'])
  108.                 msgData=msg.as_string(unixfrom=False)
  109.                 mailServer.sendmail('cube3d.designs@gmail.com', recvr, msgData)
  110.             mailServer.close()
  111.             self.sendComplete(data['header'], data['destination'])
  112.         except Exception, e:
  113.             #log.zlog.error('ERROR @ SMTP Mod:::%s'%(e))
  114.             print 'received error at %s'%e
  115.  
  116.     @classmethod
  117.     def email(self, **data):
  118.         deferToThread(self.send, data)
clone this paste RAW Paste Data