#!/usr/local/bin/python ################# # WRITTEN BY RED BYER 9/15/2013 # # Tool for gathering data on our comcast network corruption. # # Downloads a large file over our comcast network # Then Shasums the file and compares against the published hash value # to determine curruption. # # Repeat until stopped (every 30 minutes or so). # # Stores in a CSV # # USE: # Adjust your test file URL and SHA1 and path # Change the name of the localDataPath to suit your needs # Enter some run notes regarding your configuration # # OPTIONAL: Tweak the time between tests in the __main__ section. # # ################ import time import datetime from datetime import date import urllib import hashlib import os import csv #PRIMARY TEST FILE is about 1GB -------- Below is an example, this will not work #fileURL = "http://support.XXXXXXXXXX.com/XXXXXXX/XXXXXXXXXX.dmg" #knownSHA1 = "e0742e7537f80c9e01376ea3b3c09df993900643" #downloadedFilePath = "SomeUpdateFile.dmg" #DEBUG FILE File for debugging script is much smaller fileURL = "http://www.anchorageprogramming.org/wp-content/uploads/python-logo-glassy.png" knownSHA1 = "549554ef9688c8b479152a08be6a957ec06ba547" downloadedFilePath = "pythonlogo.png" localDataPath = "DownloadData-router5.csv" runnotes = "Hardwired to Netgear FVS318G router after upgrade to 3.1.1-14." def hashfile(filepath): sha1 = hashlib.sha1() f = open(filepath, 'rb') try: sha1.update(f.read()) finally: f.close() return sha1.hexdigest() class Attempt(): def __init__(self): self.starttime = '' self.endtime = '' self.downloadedhash = '' self.nates = "" def setupDownloadAttempt(self, fileURL): self.URL = fileURL; def doDownload(self): print "DOWNLOADING FROM: " + self.URL self.starttime = datetime.datetime.now() print "------START TIME: " + self.starttime.ctime() #DO the download urllib.urlretrieve(self.URL, downloadedFilePath) self.endtime = datetime.datetime.now() print "--------END TIME: " + self.endtime.ctime() #GET thehash self.downloadedhash = hashfile(downloadedFilePath) if(self.downloadedhash == knownSHA1): print "___________________________________________ FILE IS OK" self.result = "OK" else: print "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx FILE IS CORRUPT" print "Resulting hash: " + self.downloadedhash self.result = "CORRUPT" #remove the file now os.remove(downloadedFilePath) def getTraceRoute(): '''Get the traceroute for this attempt and store''' def saveAttemptInfo(self): '''Save information from this attempt as a line in CSV File''' with open(localDataPath, "a") as csvfile: csvwriter = csv.writer(csvfile, delimiter = ',', quotechar="'", quoting=csv.QUOTE_MINIMAL) csvwriter.writerow([self.starttime, self.endtime, self.downloadedhash, self.result]) os.fsync(csvfile) csvfile.close() def printIntroInfo(): print "" print "" print "##########################################" print "DOWNLOAD LARGE FILE AND TEST SHA1 CHECKSUM" print "Run Notes: " + runnotes print "Target URL: " + fileURL print "Known SHA1: " + knownSHA1 print "Data store: " + localDataPath print "##########################################" def setupDataFile(): f = open(localDataPath, "w+") f.close() with open(localDataPath, "a") as csvfile: csvwriter = csv.writer(csvfile, delimiter=',', quotechar="'", quoting=csv.QUOTE_MINIMAL) csvwriter.writerow(["LARGE FILE DOWNLOADER"]) csvwriter.writerow(["RUN NOTES:", runnotes]) csvwriter.writerow(["URL: ", fileURL]) csvwriter.writerow(["KNOWN SHA1: ", knownSHA1]) csvwriter.writerow([" "]) csvwriter.writerow(["DOWNLOAD START", "DOWNLOAD END", "SHA1", "RESULT"]) #csvfile.flush() os.fsync(csvfile) csvfile.close() if __name__ == "__main__": today = date.today() todaystr = date.today().isoformat() localDataPath = localDataPath.split()[0] + todaystr + ".csv" attemptnumber = 0 printIntroInfo() setupDataFile() while(True): attemptnumber += 1 print "------------------------------------" print "Attempt #" + str(attemptnumber) a = Attempt() a.setupDownloadAttempt(fileURL) a.doDownload() a.saveAttemptInfo() print "Sleeping for 10 minutes" print "" time.sleep(600) #sleep for 10 minutes print "Sleeping for 20 minutes more" print "" time.sleep(1200) #sleep for 10 minutes