| 1 |
ninoborges |
8 |
## Local_PST_MDB_Search.py
|
| 2 |
|
|
## This program will do a local search on the workstation's hard drive for particular file extensions. It will then
|
| 3 |
|
|
## compile these into a CSV and put them on the Y:\Log_files directory.
|
| 4 |
|
|
## EBorges
|
| 5 |
|
|
## 03.19.03
|
| 6 |
|
|
|
| 7 |
|
|
# logging added
|
| 8 |
|
|
|
| 9 |
|
|
import os,string,time,shutil,sys
|
| 10 |
|
|
from _winreg import *
|
| 11 |
|
|
|
| 12 |
|
|
|
| 13 |
|
|
## Function: addToLogFile
|
| 14 |
|
|
## Purpose: Writes the string passed in to the designated log file
|
| 15 |
|
|
## Arguments: fileName - name of the log file
|
| 16 |
|
|
## logEntry - string to add to end of log file
|
| 17 |
|
|
## userSpecific - set to 1 if log file stored in the user's netchk dir, 0 if stored globally
|
| 18 |
|
|
## echo - set to 1 to also echo the logEntry string to stdout
|
| 19 |
|
|
def addToLogFile(fileName, logEntry, userSpecific=0, echo=0 ):
|
| 20 |
|
|
# 1. Check if log file to be global or local to user
|
| 21 |
|
|
if userSpecific == 1:
|
| 22 |
|
|
# Since user specific, pull user's log directory from the registry
|
| 23 |
|
|
# ...
|
| 24 |
|
|
hkey = OpenKey( HKEY_CURRENT_USER, 'Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders' )
|
| 25 |
|
|
AppData = QueryValueEx( hkey, 'Local AppData' )
|
| 26 |
|
|
logDir = AppData[0] + '\\McGuireWoods LLP\\NetChk'
|
| 27 |
|
|
else:
|
| 28 |
|
|
# Since not user specific, use the global log directory
|
| 29 |
|
|
WinDir = os.getenv('SystemRoot')
|
| 30 |
|
|
WinDrive = string.split( WinDir, ':' )
|
| 31 |
|
|
logDir = WinDrive[0] + ':\\NetChk'
|
| 32 |
|
|
logFileName = logDir + '\\' + fileName
|
| 33 |
|
|
try:
|
| 34 |
|
|
logFile = open( logFileName, 'a' )
|
| 35 |
|
|
except IOError:
|
| 36 |
|
|
os.makedirs(logDir)
|
| 37 |
|
|
logFile = open( logFileName, 'a' )
|
| 38 |
|
|
logFile.write( time.strftime('%d/%m/%Y\t%H:%M:%S -\t',time.localtime()) + logEntry + '\n' )
|
| 39 |
|
|
if echo == 1: print logEntry
|
| 40 |
|
|
logFile.close()
|
| 41 |
|
|
|
| 42 |
|
|
## Function: listFiles
|
| 43 |
|
|
## Purpose: This is the callback for the PathWalk. It will search that particular dir for the extensions you pass
|
| 44 |
|
|
## into it.
|
| 45 |
|
|
## Arguments: arg - This should be the two extensions your looking for.
|
| 46 |
|
|
## dirname - The current directory that PathWalk passed to it.
|
| 47 |
|
|
## names - The list of file names it found in that directory.
|
| 48 |
|
|
def listFiles(arg, dirname, names):
|
| 49 |
|
|
#print "I'm now looking in %s for files with an %s or %s ext." %(dirname,arg[0],arg[1])
|
| 50 |
|
|
for x in names:
|
| 51 |
|
|
ext = x[-3:]
|
| 52 |
|
|
ext = ext.upper()
|
| 53 |
|
|
#print ext
|
| 54 |
|
|
if ext == arg[0]:
|
| 55 |
|
|
csvDir = arg[5]
|
| 56 |
|
|
file = x + "\n"
|
| 57 |
|
|
absPath = dirname + "\\" + x
|
| 58 |
|
|
#print "Found a %s file" %arg[0]
|
| 59 |
|
|
#print x
|
| 60 |
|
|
#print "Here is the Abs path..."
|
| 61 |
|
|
#print dirname
|
| 62 |
|
|
fileInfo = getFileInfo(absPath)
|
| 63 |
|
|
outputString = arg[2] + "\t" + arg[3] + "\t" + arg[4]+ "\t" + x + "\t" + dirname + "\t" + fileInfo + "\n"
|
| 64 |
|
|
o = open(csvDir + "\\Local_PST_MDB_Search.txt",'a')
|
| 65 |
|
|
o.write(outputString)
|
| 66 |
|
|
o.close()
|
| 67 |
|
|
#print outputString
|
| 68 |
|
|
#test = input("")
|
| 69 |
|
|
#searchIPF(x,dirname,arg[2])
|
| 70 |
|
|
if ext == arg[1]:
|
| 71 |
|
|
csvDir = arg[5]
|
| 72 |
|
|
file = x + "\n"
|
| 73 |
|
|
absPath = dirname + "\\" + x
|
| 74 |
|
|
#print "Found a %s file" %arg[1]
|
| 75 |
|
|
#print x
|
| 76 |
|
|
#print "Here is the abs path..."
|
| 77 |
|
|
#print dirname
|
| 78 |
|
|
print arg[5]
|
| 79 |
|
|
fileInfo = getFileInfo(absPath)
|
| 80 |
|
|
outputString = arg[2] + "\t" + arg[3] + "\t" + arg[4]+ "\t" + x + "\t" + dirname + "\t" + fileInfo + "\n"
|
| 81 |
|
|
o = open(csvDir + "\\Local_PST_MDB_Search.txt",'a')
|
| 82 |
|
|
o.write(outputString)
|
| 83 |
|
|
o.close()
|
| 84 |
|
|
#print outputString
|
| 85 |
|
|
#test = input("")
|
| 86 |
|
|
#searchIPF(x,dirname,arg[2])
|
| 87 |
|
|
|
| 88 |
|
|
## Function: getFileInfo
|
| 89 |
|
|
## Purpose: This function is called by listFiles when listFiles finds a file that matches the ext. This
|
| 90 |
|
|
## function will get file information from the found file. Its this info that gets into the Csv file.
|
| 91 |
|
|
## Arguments: absPath - The absolute path to the file that was found.
|
| 92 |
|
|
## Returns: results - A string with the following info: Created Date, Modified Date and Size of the file in KB.
|
| 93 |
|
|
def getFileInfo(absPath):
|
| 94 |
|
|
# Formatting:
|
| 95 |
|
|
# date / time: mm/dd/yyyy hh:mm
|
| 96 |
|
|
createdDateTup = time.gmtime(os.stat(absPath)[9])
|
| 97 |
|
|
createdDate = str(createdDateTup[1]) + '/' + str(createdDateTup[2]) + '/' + str(createdDateTup[0]) + ' ' + str(createdDateTup[3]) + ':' + str(createdDateTup[4])
|
| 98 |
|
|
modifiedDateTup = time.gmtime(os.stat(absPath)[8])
|
| 99 |
|
|
modifiedDate = str(modifiedDateTup[1]) + '/' + str(modifiedDateTup[2]) + '/' + str(modifiedDateTup[0]) + ' ' + str(modifiedDateTup[3]) + ':' + str(modifiedDateTup[4])
|
| 100 |
|
|
size = str(os.stat(absPath)[6]/1024)
|
| 101 |
|
|
results = size + "\t" + createdDate + "\t" + modifiedDate
|
| 102 |
|
|
return results
|
| 103 |
|
|
|
| 104 |
|
|
## Function: getRegInfo
|
| 105 |
|
|
## Purpose: This function will give you the value of a key pased into it.
|
| 106 |
|
|
## Arguments: handle - One of the HKEY* constants
|
| 107 |
|
|
## keyPath - The path to the key you want the value for.
|
| 108 |
|
|
## keyName - The name of the key you want the value of.
|
| 109 |
|
|
## Returns: result - The string value of the key
|
| 110 |
|
|
def getRegInfo(handle,keyPath,keyName):
|
| 111 |
|
|
keyObj = OpenKey(handle,keyPath)
|
| 112 |
|
|
result = QueryValueEx(keyObj,keyName)
|
| 113 |
|
|
keyObj.Close()
|
| 114 |
|
|
result = result[0]
|
| 115 |
|
|
return result
|
| 116 |
|
|
|
| 117 |
|
|
## Function: reportResults
|
| 118 |
|
|
## Purpose: This is a small function that will simple copy a file from location a to location be. I just did
|
| 119 |
|
|
## this so that I could call it twice.
|
| 120 |
|
|
## Arguments: copyFrom - The full path of the file
|
| 121 |
|
|
## copyTo - The full path of the destination
|
| 122 |
|
|
## Returns: result - just an error code to see if it worked or not.
|
| 123 |
|
|
def reportResults(copyFrom,copyTo,newFileName):
|
| 124 |
|
|
#print "trying to copy %s to %s" %(copyFrom,copyTo)
|
| 125 |
|
|
try:
|
| 126 |
|
|
if not os.path.exists(copyTo):
|
| 127 |
|
|
addToLogFile( logFile, 'The Log_Files path didnt exist. Creating a new one...')
|
| 128 |
|
|
#print "path didnt exist. creating"
|
| 129 |
|
|
os.mkdir(copyTo)
|
| 130 |
|
|
except:
|
| 131 |
|
|
pass
|
| 132 |
|
|
try:
|
| 133 |
|
|
addToLogFile( logFile, "Trying to copy %s to %s" %(copyFrom,copyTo))
|
| 134 |
|
|
copyTo = copyTo + newFileName
|
| 135 |
|
|
shutil.copyfile(copyFrom,copyTo)
|
| 136 |
|
|
addToLogFile( logFile, 'Report has been copied from %s to %s' %(copyFrom,copyTo))
|
| 137 |
|
|
#print "copied"
|
| 138 |
|
|
result = "copied"
|
| 139 |
|
|
except:
|
| 140 |
|
|
addToLogFile( logFile, 'I was not able to copy the report! Exiting program. Will try on next reboot.')
|
| 141 |
|
|
#print "not copied"
|
| 142 |
|
|
result = "not_copied"
|
| 143 |
|
|
return result
|
| 144 |
|
|
|
| 145 |
|
|
## Function: cleanUp
|
| 146 |
|
|
## Purpose: This is the final clean up. This will remove the registry information and clean up after itself.
|
| 147 |
|
|
## Arguments: currentDir - the path and name of the exe so that it can delete it.
|
| 148 |
|
|
def cleanUp(currentDir):
|
| 149 |
|
|
# The script is done looking for files. Remove SELF from Run key in registry and report findings.
|
| 150 |
|
|
removeCommand = 'c:\\winnt\\system32\\cmd.exe /c "del %s"' %(currentDir + "Local_PST_MDB_Search.exe")
|
| 151 |
|
|
runKey = OpenKey(HKEY_LOCAL_MACHINE,r"Software\Microsoft\Windows\CurrentVersion\Run",0,KEY_WRITE)
|
| 152 |
|
|
DeleteValue(runKey, "Local_PST_MDB_Search")
|
| 153 |
|
|
runKey.Close()
|
| 154 |
|
|
tempKey = OpenKey(HKEY_CURRENT_USER,r"Software\McGuireWoods LLP\Temp",0,KEY_WRITE)
|
| 155 |
|
|
DeleteValue(tempKey,"Local_PST_MDB_Search_Status")
|
| 156 |
|
|
tempKey.Close()
|
| 157 |
|
|
runOnceKey = OpenKey(HKEY_LOCAL_MACHINE,r"Software\Microsoft\Windows\CurrentVersion\RunOnce",0,KEY_WRITE)
|
| 158 |
|
|
SetValueEx(runOnceKey,"Local_PST_MDB_Search","",1,removeCommand)
|
| 159 |
|
|
runOnceKey.Close()
|
| 160 |
|
|
print "Done!"
|
| 161 |
|
|
|
| 162 |
|
|
if __name__ == '__main__':
|
| 163 |
|
|
# set log file name
|
| 164 |
|
|
logFile = 'Local_PST_MDB_Search.log'
|
| 165 |
|
|
userSpecific = 0
|
| 166 |
|
|
currentDir = os.getcwd()
|
| 167 |
|
|
alreadyRan = ""
|
| 168 |
|
|
office = ""
|
| 169 |
|
|
# Add SELF to Run key in Reg incase they reboot.
|
| 170 |
|
|
addToLogFile( logFile, 'STARTING Local_PST_MDB_Search.py...')
|
| 171 |
|
|
addToLogFile( logFile, 'Entering myself into the Run key of the registry in case Im stopped before I finish...')
|
| 172 |
|
|
runKey = OpenKey(HKEY_LOCAL_MACHINE,r"Software\Microsoft\Windows\CurrentVersion\Run",0,KEY_WRITE)
|
| 173 |
|
|
SetValueEx(runKey,"Local_PST_MDB_Search","",1,currentDir + "\\Local_PST_MDB_Search.exe")
|
| 174 |
|
|
runKey.Close()
|
| 175 |
|
|
addToLogFile( logFile, 'Local_PST_MDB_Search.exe is now in the Run key of the registry.')
|
| 176 |
|
|
# Wait 5 minutes.
|
| 177 |
|
|
addToLogFile( logFile, 'Waiting 5 minutes so that system can start up normally...')
|
| 178 |
|
|
#print "Running... Waiting 5 minutes"
|
| 179 |
|
|
time.sleep(300)
|
| 180 |
|
|
#print "5 minutes are up. Now running search"
|
| 181 |
|
|
addToLogFile( logFile, '5 minutes is up. Starting search.')
|
| 182 |
|
|
# Get the machine name, user name and office from the registry.
|
| 183 |
|
|
machineName = getRegInfo(HKEY_LOCAL_MACHINE,r"System\CurrentControlSet\Control\ComputerName\ComputerName","ComputerName")
|
| 184 |
|
|
userName = getRegInfo(HKEY_CURRENT_USER,r"Software\Microsoft\Windows\CurrentVersion\Explorer","Logon User Name")
|
| 185 |
|
|
try:
|
| 186 |
|
|
office = getRegInfo(HKEY_CURRENT_USER,r"Network\Y","RemotePath")
|
| 187 |
|
|
office = (office.split("\\"))[2]
|
| 188 |
|
|
except:
|
| 189 |
|
|
addToLogFile( logFile, 'ERROR: I was not able to get the find the Y drive! I wont know the office!')
|
| 190 |
|
|
pass
|
| 191 |
|
|
mainDrive = "c:\\"
|
| 192 |
|
|
log_FilesDir = "y:\\log_files\\PST_MDB_Search\\"
|
| 193 |
|
|
log_FileName = machineName + ".txt"
|
| 194 |
|
|
# Extensions of the files you're looking for.
|
| 195 |
|
|
fileEXT = ('MDB','PST')
|
| 196 |
|
|
# Wrap the extra reporting info into the arg varible
|
| 197 |
|
|
extraInfo = (machineName,userName,office,currentDir)
|
| 198 |
|
|
arguments = fileEXT + extraInfo
|
| 199 |
|
|
try:
|
| 200 |
|
|
addToLogFile( logFile, 'Checking to see if I have already run...')
|
| 201 |
|
|
alreadyRan = getRegInfo(HKEY_CURRENT_USER,r"Software\McGuireWoods LLP\Temp","Local_PST_MDB_Search_Status")
|
| 202 |
|
|
except:
|
| 203 |
|
|
addToLogFile( logFile, 'I have not already run. Running for the first time...')
|
| 204 |
|
|
pass
|
| 205 |
|
|
if alreadyRan:
|
| 206 |
|
|
addToLogFile( logFile, 'I have already done a full search. I will only try to copy to the Log_files dir.')
|
| 207 |
|
|
counter = int(alreadyRan)
|
| 208 |
|
|
if counter > 15:
|
| 209 |
|
|
addToLogFile( logFile, 'ERROR: I have tried to copy to the Log_Files dir more than 15 times. Exiting with error.')
|
| 210 |
|
|
cleanUp(currentDir)
|
| 211 |
|
|
sys.exit()
|
| 212 |
|
|
print "I already ran"
|
| 213 |
|
|
status = reportResults(currentDir + '\\Local_PST_MDB_Search.txt', log_FilesDir,log_FileName)
|
| 214 |
|
|
print "just tried to copy"
|
| 215 |
|
|
if status == "copied":
|
| 216 |
|
|
#print "Copied sucessfully"
|
| 217 |
|
|
addToLogFile( logFile, 'Report copy completed! Cleaning up registry...')
|
| 218 |
|
|
cleanUp(currentDir)
|
| 219 |
|
|
addToLogFile( logFile, 'Registry clean! Program completed!')
|
| 220 |
|
|
#print "cleaned up. exiting script"
|
| 221 |
|
|
sys.exit()
|
| 222 |
|
|
else:
|
| 223 |
|
|
increment = str(alreadyRan)
|
| 224 |
|
|
increment = string.atoi(increment)
|
| 225 |
|
|
increment = increment + 1
|
| 226 |
|
|
increment = str(increment)
|
| 227 |
|
|
addToLogFile( logFile, 'I still cant get to the Y drive. Incrementing the registry to a run max of 15')
|
| 228 |
|
|
tempKey = CreateKey(HKEY_CURRENT_USER,r"Software\McGuireWoods LLP\Temp")
|
| 229 |
|
|
SetValueEx(tempKey,"Local_PST_MDB_Search_Status","",1,increment)
|
| 230 |
|
|
tempKey.Close()
|
| 231 |
|
|
sys.exit()
|
| 232 |
|
|
# Clear the file, incase I'm running again.
|
| 233 |
|
|
c = open(currentDir + '\\Local_PST_MDB_Search.txt','w')
|
| 234 |
|
|
c.close()
|
| 235 |
|
|
os.path.walk(mainDrive,listFiles,arguments)
|
| 236 |
|
|
addToLogFile( logFile, 'Search Complete!')
|
| 237 |
|
|
# The search portion is done. Flip a bit in the registry telling this script not to re-Run from the start.
|
| 238 |
|
|
tempKey = CreateKey(HKEY_CURRENT_USER,r"Software\McGuireWoods LLP\Temp")
|
| 239 |
|
|
SetValueEx(tempKey,"Local_PST_MDB_Search_Status","",1,"1")
|
| 240 |
|
|
tempKey.Close()
|
| 241 |
|
|
addToLogFile( logFile, 'Copying the finished report to Y:Log_Files...')
|
| 242 |
|
|
status = reportResults(currentDir + '\\Local_PST_MDB_Search.txt', log_FilesDir,log_FileName)
|
| 243 |
|
|
if status == "copied":
|
| 244 |
|
|
addToLogFile( logFile, 'Report Copied! Cleaning up the registry...')
|
| 245 |
|
|
cleanUp(currentDir)
|
| 246 |
|
|
addToLogFile( logFile, 'Registry Clean! Program completed!')
|
| 247 |
|
|
else:
|
| 248 |
|
|
addToLogFile( logFile, 'Couldent copy file. Will try again on the next reboot...')
|
| 249 |
|
|
pass |