| 1 |
ninoborges |
8 |
## PST_Search.py
|
| 2 |
|
|
## This program will search across all accessable shares on all applicable servers for a particular file extension.
|
| 3 |
|
|
## It will bring back results in the form of an inventory of all matching files. (In this case it will retrieve a list
|
| 4 |
|
|
## of all MDB and PST files.
|
| 5 |
|
|
## EBorges
|
| 6 |
|
|
## 03.10.03
|
| 7 |
|
|
|
| 8 |
|
|
import os,string,time
|
| 9 |
|
|
from win32pipe import popen3
|
| 10 |
|
|
|
| 11 |
|
|
## Function: getShares
|
| 12 |
|
|
## Purpose: This function will return a list of all of the shares on a particular server.
|
| 13 |
|
|
## Arguments: server - The server it will query for shares.
|
| 14 |
|
|
## Returns: retval - A list of shares on server.
|
| 15 |
|
|
def getShares(server):
|
| 16 |
|
|
" return list of shares on a target server"
|
| 17 |
|
|
retval = []
|
| 18 |
|
|
cmd = "net view %s" % server
|
| 19 |
|
|
shares = popen3(cmd)[1].readlines()
|
| 20 |
|
|
for share in shares:
|
| 21 |
|
|
shareInfo = string.split(share)
|
| 22 |
|
|
try:
|
| 23 |
|
|
shareName = string.join(shareInfo[0:shareInfo.index('Disk')],'')
|
| 24 |
|
|
if len(shareName) != 1:
|
| 25 |
|
|
retval.append(shareName)
|
| 26 |
|
|
except:
|
| 27 |
|
|
pass
|
| 28 |
|
|
return retval
|
| 29 |
|
|
|
| 30 |
|
|
## Function: listFiles
|
| 31 |
|
|
## Purpose: This is the callback for the PathWalk. It will search that particular dir for the extensions you pass
|
| 32 |
|
|
## into it.
|
| 33 |
|
|
## Arguments: arg - This should be the two extensions your looking for.
|
| 34 |
|
|
## dirname - The current directory that PathWalk passed to it.
|
| 35 |
|
|
## names - The list of file names it found in that directory.
|
| 36 |
|
|
def listFiles(arg, dirname, names):
|
| 37 |
|
|
#print "I'm now looking in %s for files with an %s or %s ext." %(dirname,arg[0],arg[1])
|
| 38 |
|
|
serverName = dirname.split("\\")[2]
|
| 39 |
|
|
for x in names:
|
| 40 |
|
|
ext = x[-3:]
|
| 41 |
|
|
ext = ext.upper()
|
| 42 |
|
|
#print ext
|
| 43 |
|
|
if ext == arg[0]:
|
| 44 |
|
|
file = x + "\n"
|
| 45 |
|
|
absPath = dirname + "\\" + x
|
| 46 |
|
|
#o.write(file)
|
| 47 |
|
|
#print "Found a %s file" %arg[0]
|
| 48 |
|
|
#print x
|
| 49 |
|
|
#print "Here is the Abs path..."
|
| 50 |
|
|
#print dirname
|
| 51 |
|
|
fileInfo = getFileInfo(absPath)
|
| 52 |
|
|
outputString = serverName + "," + x + "," + dirname + "," + fileInfo + "\n"
|
| 53 |
|
|
o = open(r"U:\PST_MDB_Search.csv",'a')
|
| 54 |
|
|
o.write(outputString)
|
| 55 |
|
|
o.close()
|
| 56 |
|
|
#print outputString
|
| 57 |
|
|
#test = input("")
|
| 58 |
|
|
#searchIPF(x,dirname,arg[2])
|
| 59 |
|
|
if ext == arg[1]:
|
| 60 |
|
|
file = x + "\n"
|
| 61 |
|
|
absPath = dirname + "\\" + x
|
| 62 |
|
|
#o.write(file)
|
| 63 |
|
|
#print "Found a %s file" %arg[1]
|
| 64 |
|
|
#print x
|
| 65 |
|
|
#print "Here is the abs path..."
|
| 66 |
|
|
#print dirname
|
| 67 |
|
|
fileInfo = getFileInfo(absPath)
|
| 68 |
|
|
outputString = serverName + "," + x + "," + dirname + "," + fileInfo + "\n"
|
| 69 |
|
|
o = open(r"U:\PST_MDB_Search.csv",'a')
|
| 70 |
|
|
o.write(outputString)
|
| 71 |
|
|
o.close()
|
| 72 |
|
|
#print outputString
|
| 73 |
|
|
#test = input("")
|
| 74 |
|
|
#searchIPF(x,dirname,arg[2])
|
| 75 |
|
|
## Function: getFileInfo
|
| 76 |
|
|
## Purpose: This function is called by listFiles when listFiles finds a file that matches the ext. This
|
| 77 |
|
|
## function will get file information from the found file. Its this info that gets into the Csv file.
|
| 78 |
|
|
## Arguments: absPath - The absolute path to the file that was found.
|
| 79 |
|
|
## Returns: results - A string with the following info: Created Date, Modified Date and Size of the file in KB.
|
| 80 |
|
|
def getFileInfo(absPath):
|
| 81 |
|
|
createdDate = str(time.ctime(os.stat(absPath)[9]))
|
| 82 |
|
|
modifiedDate = str(time.ctime(os.stat(absPath)[8]))
|
| 83 |
|
|
size = str(os.stat(absPath)[6]/1024)
|
| 84 |
|
|
results = size + "Kb" + "," + createdDate + "," + modifiedDate
|
| 85 |
|
|
return results
|
| 86 |
|
|
|
| 87 |
|
|
if __name__ == '__main__':
|
| 88 |
|
|
# Extensions of the files you're looking for.
|
| 89 |
|
|
fileEXT = ('MDB','PST')
|
| 90 |
|
|
# All of the file servers that we need to do the search on.
|
| 91 |
|
|
allServers = ('ric1','ric3','ric4','ric5','ric6','ric7','ric8','atl','bal','cha','chi','clt','jac','nor','nyc','pit'
|
| 92 |
|
|
,'tys','was','ATLBDC','BALBDC','CHABDC','CHIBDC','CLTBDC','JACBDC','NORBDC','NYCBDC','PITBDC',
|
| 93 |
|
|
'TYSBDC','TYSBDC1','WASBDC','bru')
|
| 94 |
|
|
for server in allServers:
|
| 95 |
|
|
server = r"\\"+ server
|
| 96 |
|
|
print "Getting all shares located on %s" % server
|
| 97 |
|
|
shareList = getShares(server)
|
| 98 |
|
|
#print shares
|
| 99 |
|
|
#test = input("")
|
| 100 |
|
|
for share in shareList:
|
| 101 |
|
|
share = server + "\\"+ share
|
| 102 |
|
|
os.path.walk(share,listFiles,fileEXT)
|