import os def inandout(compname): #This function removes a computer that has been thoroughly searched from the list of computers to search so that if the the search takes more #than one execution, then it won't be redundant. Brines = open("complist.txt").readlines() Lines = open("complist.txt", "w") i = 0 print "LEN: ", len(Brines) while i < len(Brines): #print i if compname != Brines[i][:-1].split()[0]: #print "|" + compname + "| != |" + Brines[i][:-1].split()[0] + "|" Lines.write(Brines[i]) i = i + 1 Lines.close() return 1 def lowercase(input): return input.lower() def notin(name): #################################################################################################################################################################### ###############Specification for directories not to enter########################################################################################################### badirs = [".", "..", "Program Files", "WINDOWS", "Default User", "temp", "All Users", "i386", "WINNT", "Perl", "Start Menu"] #badirs = badirs + ["Documents and Settings", "filebox"] # for testing purposes to speed up search process. ##################################################################################################################################################################### i = 0 name = name[:-1] while i < len(badirs): if lowercase(name) == lowercase(badirs[i]) or lowercase(name[:len(badirs[i])]) == lowercase(badirs[i]): return 0 i = i + 1 return 1 def latestdirfind(input): i = len(input)-2 while i > -1: if input[i] == "\\": return input[i+1:] i = i - 1 return input def extchk(single): ################################################################################################ #################Specification for filetypes to search for###################################### extensions = ["mp3", "shn", "wav", "hnf", "m4a", "hnf", "lac", "ape", "wma", "ogg"]############# ################################################################################################ i = 0 while i < len(extensions): if lowercase(single) == extensions[i]: return 1 i = i + 1 return 0 def FileNameSearch(filename, keyword): #See if a keyword is contained if keyword == "": return 1 i = 0 while i < len(filename)-len(keyword)-1: #print filename if filename[i:i+len(keyword)] == keyword: return 1 i = i + 1 return 0 def concatenate(comp, dir, newdir): #This Function properly formats the output the user sees most of the time - the name of the directory currently being searched if len(comp) + len(dir) + len(newdir) > 79: return comp + dir[:75-len(newdir) - len(comp)] + "...\\" + newdir return comp + dir + newdir def conneckt0r(host): pinger = "ping -n 1 " + host + " > c:\\ping.txt" os.system(pinger) res = open("c:\\ping.txt").readlines() #import list of files/folders in working directory. os.system("del c:\\ping.txt")#remove list of files/folders in working directory. if res[0][:22] == "Ping request could not": return 0 #print res[0][:22] return 1 def CompParse(dirname, drivename, compname, copyto): dircomm= """dir \"""" + drivename + dirname + """\" > c:\yay.txt""" # send list of files/folders to a txt file for reading. #print "dirname: |" + dirname + "|" #print "dircomm: " + dircomm x = os.system(dircomm) dir = open("c:\yay.txt").readlines() #import list of files/folders in working directory. dir = dir[5:] os.system("del c:\yay.txt")#remove list of files/folders in working directory. if x==0: #print "CONNECT! Successful to" + compname #print dir i = 0 while i < len(dir): line = dir[i].split() if line[3] == "": newdirname = "" p = 4 while p < len(line): newdirname = newdirname + " " + line[p] p = p + 1 newdirname= newdirname[1:] newdirname = newdirname + "\\" if notin(newdirname) == 1: print concatenate(compname, dirname, newdirname) CompParse(dirname+newdirname, drivename, compname, copyto) elif len(line) > 4: filename = line[4] if len(line) > 5: x = 5 while x < len(line): filename = filename + " " + line[x] x = x + 1 #print filename extension = filename[-3:] #print extension ############################################################################################################# searchFileName=""# makes it only search for files containing this string Asterisk Wildcard NOT implemented ############################################################################################################# if extchk(extension) == 1: if FileNameSearch(filename, searchFileName) == 1: #print "Music File Found!!!" latestdir = latestdirfind(dirname) mkdircomm = "mkdir \"" + copyto + latestdir + "\"" #make a directory to put files into so we dont have just a bunch of files in one directory. Directory is named same as directory the file is in remotely. print mkdircomm os.system(mkdircomm) copycomm= """copy \"""" + drivename + dirname + filename + "\" \"" + copyto + latestdir +"\"" print copycomm os.system(copycomm) #Copy command i = i + 1 return 1 else: print "Unable to connect to " + dirname + " on " + compname return 0 #os.system("net use o: /delete") Lines = open("complist.txt").readlines() #import list of computers Lines = Lines[1:] i = 0 ##################################### drivename = "D:" # Drive Letter you want to map to locally copyto = "c:\mp3s\\" #Where to dump the files you find ##################################### while i < len(Lines): if conneckt0r(Lines[i].split()[0])==1: Connect = "net use " + drivename + " \\\\" + Lines[i].split()[0] + "\c$ /user:username pass" #map drive for searching print Connect os.system(Connect) success = CompParse("\\", drivename, Lines[i].split()[0], copyto) if success == 1: inandout(Lines[i].split()[0]) #if we finished searching the computer then we can delete the computer from the list of computers to search. #This makes it so if the search takes more than one execution it won't be redundant. Disconnect= "net use " + drivename + " /delete" #unmap drive so we can reconnect to next compuer print Disconnect os.system(Disconnect) else: print "COMPUTER UNAVAILABLE: " + Lines[i].split()[0] i = i + 1