| 1 |
ninoborges |
8 |
## This program will alert when it finds that a user, who's machine name doesn't
|
| 2 |
|
|
## match their username, has logged into that non-matching computer 2 out of 2
|
| 3 |
|
|
## times in one week. This program will take a list of machine names that don't
|
| 4 |
|
|
## match usernames, strip out entries created by anyone in the "exclude" list,
|
| 5 |
|
|
## and then alert if it finds someone who keeps logging into a particular machine.
|
| 6 |
|
|
## Idealy this program will run this entire process, including the query and export
|
| 7 |
|
|
## in SMS, automatically every month.
|
| 8 |
|
|
#EBorges
|
| 9 |
|
|
#05.31.02
|
| 10 |
|
|
|
| 11 |
|
|
import os, sys, string
|
| 12 |
|
|
|
| 13 |
|
|
#########################################################################################
|
| 14 |
|
|
## This will take a Dictionary and delete all key:value pairs that are in the exceptList
|
| 15 |
|
|
#########################################################################################
|
| 16 |
|
|
def ExcludeThese(dupDict):
|
| 17 |
|
|
try:
|
| 18 |
|
|
f=open('Duplicate_machines_Except_list\\exept_list.txt','r')
|
| 19 |
|
|
exceptList = f.readlines()
|
| 20 |
|
|
f.close()
|
| 21 |
|
|
except IOError:
|
| 22 |
|
|
print "HEY! I need an Except list!"
|
| 23 |
|
|
for x in exceptList:
|
| 24 |
|
|
x,null = string.split(x,'\n')
|
| 25 |
|
|
if dupDict.has_key(x):
|
| 26 |
|
|
del dupDict[x]
|
| 27 |
|
|
return dupDict
|
| 28 |
|
|
|
| 29 |
|
|
###############################################################################################
|
| 30 |
|
|
##GetList will open every file in the query directory and create a list of lists in the varible
|
| 31 |
|
|
## fileContentsList.
|
| 32 |
|
|
###############################################################################################
|
| 33 |
|
|
def GetList(dir,fileList):
|
| 34 |
|
|
fileContentsList = []
|
| 35 |
|
|
dir = dir + "\\"
|
| 36 |
|
|
for file in fileList:
|
| 37 |
|
|
file = dir + file
|
| 38 |
|
|
f=open(file,'r')
|
| 39 |
|
|
fileContentsList.append(f.readlines())
|
| 40 |
|
|
f.close()
|
| 41 |
|
|
return fileContentsList
|
| 42 |
|
|
|
| 43 |
|
|
############################################################################################
|
| 44 |
|
|
##MakeDict will take that list of lists, insert it into a matrix and pull out all duplicate
|
| 45 |
|
|
## key:Value pairs. It will put these into dupDict for further processing.
|
| 46 |
|
|
############################################################################################
|
| 47 |
|
|
def MakeDict(fileContentsList):
|
| 48 |
|
|
matrix = {}
|
| 49 |
|
|
dupDict = {}
|
| 50 |
|
|
for list in fileContentsList:
|
| 51 |
|
|
for line in list:
|
| 52 |
|
|
newlist = string.split(line,',')
|
| 53 |
|
|
if matrix.has_key(newlist[0]):
|
| 54 |
|
|
if matrix.get(newlist[0]) == newlist[1]:
|
| 55 |
|
|
dupDict[newlist[0]] = (newlist[1],newlist[2])
|
| 56 |
|
|
matrix[newlist[0]]=newlist[1]
|
| 57 |
|
|
return dupDict
|
| 58 |
|
|
|
| 59 |
|
|
######################################################################################
|
| 60 |
|
|
##This is the main section that calls the functions above and creates the output file.
|
| 61 |
|
|
######################################################################################
|
| 62 |
|
|
if __name__=='__main__':
|
| 63 |
|
|
queryDir = 'U:\\duplicate_machine_queries'
|
| 64 |
|
|
fileList = os.listdir(queryDir)
|
| 65 |
|
|
fileContentsList=GetList(queryDir,fileList)
|
| 66 |
|
|
fileDict = MakeDict(fileContentsList)
|
| 67 |
|
|
FinalDict = ExcludeThese(fileDict)
|
| 68 |
|
|
print "Creating a file with the results of your query."
|
| 69 |
|
|
print "This file will be put on your U drive under the name of"
|
| 70 |
|
|
print "Duplicate_machine_query_output.csv"
|
| 71 |
|
|
o=open('U:\\Duplicate_machine_query_output.csv','w')
|
| 72 |
|
|
for key in FinalDict.keys():
|
| 73 |
|
|
line = key,FinalDict.get(key)
|
| 74 |
|
|
line = line [0] + "," + line[1][0] +"," + line[1][1] + "\n"
|
| 75 |
|
|
o.write(line)
|
| 76 |
|
|
o.close()
|
| 77 |
|
|
print "The output file has been written." |