|
#!/usr/bin/python # The script saves a Webserverlisting into a file and afterwards # compares the online content with the file # Tested with Apache standard configuration only # If a change is made you will be informed once and then # the offline file will be updated # Change theurl, username, password and fileoffline to fit your needs. # Also change the fromaddr and the toaddrs array. # Afterwards start the script once with initialload set to 1. # The local file will be initialized. # Then reset the initialload value to 0 and start the script # periodically (e.g. crontab) # If this is done get a good book about some Unix stuff, # sit down on your favourite chair and wait for notification ;) from HTMLParser import HTMLParser import urllib2 import smtplib
initialload = 0 theurl = 'https://yourdomain.com' username = 'username' password = 'password' fileoffline = '/local/path/to/file.dat'
def mail_send(msg): fromaddr = "serveradmin@yourdomain.com" toaddrs = ("firstinform@domain.com","secondinform@domain.com") server = smtplib.SMTP('localhost') server.sendmail(fromaddr, toaddrs, msg) server.quit() #dont change anything beyond this line
filearrayonline = [] filearrayoffline = [] months = 'Jan','Feb','Mar','Apr','May','Jun','Jul', \ 'Aug','Sep','Oct','Nov','Dec' assozarray = []
class AnchorParser(HTMLParser): def __init__(self, url): HTMLParser.__init__(self) def handle_starttag(self, tag, attrs): if tag =='a': for key, value in attrs: if key == 'href': if '?' not in value: filearrayonline.append(value.strip()) def handle_data(self,datain): for month in months: if month in datain: filearrayonline.append(datain.strip())
passman = urllib2.HTTPPasswordMgrWithDefaultRealm() passman.add_password(None, theurl, username, password) authhandler = urllib2.HTTPBasicAuthHandler(passman) opener = urllib2.build_opener(authhandler) urllib2.install_opener(opener)
parser = AnchorParser(HTMLParser) data = urllib2.urlopen(theurl).read() parser.feed(data) print filearrayonline
if initialload == 1: f1 = file(fileoffline,"w") for fileon in filearrayonline: f1.write(fileon + "\n") f1.close()
f1 = file(fileoffline,"r")
for line in f1: filearrayoffline.append(line.rstrip("\r\n"))
f1.close()
for fileon in filearrayonline: if fileon not in filearrayoffline: mail_send("New file in maths download") print "New files online" f1 = file(fileoffline,"w") for fileon1 in filearrayonline: f1.write(fileon1 + "\n") f1.close() else: print "No new files"
|