#!/usr/bin/python import urllib, string, sys, os, smtplib from xml.dom import minidom ####################### START USER DEFINED VARIABLES ####################### #comma-separated list of RSS feeds to monitor feeds = ['http://rss.cnn.com/rss/cnn_topstories.rss', 'http://digg.com/rss/containertechnology.xml'] #strings to look for in the feeds #for a pattern to match, all words in each list must be present #in the search string patterns = [['your', 'full', 'name'], ['something', 'else']] #mailing list for the alerts tolist = ['you@mail.com', 'yourfriend@mail.com'] #email address that you want the alerts to come from fromaddr = 'feedmonitor@somewhere.com' ####################### END USER DEFINED VARIABLES ####################### #function to filter whether or not a phrase contains all the keywords #in a particular pattern -> only returns true if all the words in #the pattern are present def filter(phrase): for pattern in patterns: alert = True postWords = string.split(string.lower(phrase)) for word in pattern: if word not in postWords: alert = False if alert: return True return False #container for the items to monitor posts = [] #container for items to alert on alerts = [] for f in feeds: #to use the Python XML parser, we need to place the RSS data in a file fp = open("tempfile","w") fp.write(urllib.urlopen(f).read()) fp.close() try: xmldoc = minidom.parse("tempfile") except: print "feedMonitor - unable to read feed:", f sys.exit() items = xmldoc.getElementsByTagName("item") for i in items: myTitle = i.getElementsByTagName("title") myLink = i.getElementsByTagName("link") #each post is a tuple containing the source, link, and title of the vuln posts.append((f, myLink[0].firstChild.toxml(), myTitle[0].firstChild.toxml())) #send each post to the keyword filter #any posts that match on one or more filters will be added to the list of alerts #to be mailed for post in posts: if filter(post[2]): alerts.append(post) #if there are any alerts found, mail them out if len(alerts): s = smtplib.SMTP("localhost") msg = 'To: ' for recipient in tolist[:-1]: msg += recipient+', ' msg += tolist[-1] if len(alerts) == 1: msg += '\nSubject: New Alert Found - Please Review\n\n' else: msg += '\nSubject: New Alerts Found - Please Review\n\n' for alert in alerts: msg += alert[2] + " - " + alert[1] + "\n\n" #send out the mail print msg print tolist s.sendmail(fromaddr,tolist,msg) #a little housecleaning - remove the tempfile try: os.remove("tempfile") except: pass