1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
| import winreg, hashlib, csv, os.path
from ast import literal_eval
sList = {} ; mList = {};
def mGet(mName):
global sList, mList
tmpList = []
regPath = ["SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall",
"SOFTWARE\\WOW6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall"]
distReg = winreg.ConnectRegistry("\\\\" + mName, winreg.HKEY_LOCAL_MACHINE)
for path in regPath:
listKey = winreg.OpenKey(distReg, path)
i = 0
while True:
try:
sName = ""
sKName = winreg.EnumKey(listKey, i)
sK = winreg.OpenKey(listKey, sKName)
try:
sName = winreg.QueryValueEx(sK, "DisplayName")[0]
except:
try:
sName = winreg.QueryValueEx(sK, "QuietDisplayName")[0]
except:
pass
sNameHash = hashlib.md5(sName.encode("utf-8")).digest()[:4]
sID = str(int.from_bytes(sNameHash, "big")).zfill(10)
if sName and "Update" not in sName:
if sID not in sList:
sList[sID] = sName.encode("utf-8").decode("cp1252")
tmpList.append(sID)
i += 1
except WindowsError as e:
break
sList = dict([(k,v) for v,k in sorted([(v,k) for k,v in sList.items()])])
tmpList = sorted(tmpList, key=lambda x: list(sList.keys()).index(x))
mList[mName] = tmpList
print("La liste des logiciels a bien été récupérée sur le poste " + mName + "\n")
def sh_sList(mName=None):
if mName in mList:
trgt = mList[mName]
elif not mName:
trgt = sList
else:
print("La machine \"" + mName + "\" n'a as été scannée !!" +
"\nEffectuez un scan manuel sur celle-ci, et réessayez")
return
print("softID softName\n" + "-"*60)
for sID in trgt:
print(sID + " " + sList[sID])
print("\n" + str(len(sList)) + " logiciels ont été répertoriés\n")
def search(softKw):
if not sList:
print("Merci d'effectuer un scan préalable des machines...")
fnd = False
for m in mList:
for sID in mList[m]:
if softKw in sList[sID]:
print(softKw + " est bien présent sur le poste " + m); fnd = True; break
if not fnd:
print("Ancun résultats de trouvé concernant " + softKw)
print()
def expt(xList):
if xList in (sList, mList):
if type(next(iter(xList.values()))) is str:
xType = "sList"
else:
xType= "mList"
with open("export_" + xType + ".csv", "w", newline="") as csvfile:
wr = csv.writer(csvfile, delimiter=";",
quotechar="|", quoting=csv.QUOTE_MINIMAL)
for x in xList:
wr.writerow([x, xList[x]])
print(xType + " a bien été exportée dans le fichier : " + "export_" + xType + ".csv")
else:
print("On ne peut exporter que s(oft)List ou m(achine)List !!")
def impt(xList):
global sList, mList
if xList is "s" and os.path.isfile("export_sList.csv"):
with open("export_sList.csv", mode="r") as csvfile:
rdr = csv.reader(csvfile, delimiter=";",
quotechar="|", quoting=csv.QUOTE_MINIMAL)
sList = {rows[0]:rows[1] for rows in rdr}
print("Import effectué : liste des logiciels")
elif xList is "m" and os.path.isfile("export_mList.csv"):
with open("export_mList.csv", mode="r") as csvfile:
rdr = csv.reader(csvfile, delimiter=";",
quotechar="|", quoting=csv.QUOTE_MINIMAL)
mList = {rows[0]:literal_eval(rows[1]) for rows in rdr}
print("Import effectué : liste des logiciels installés sur les machines")
print()
print(" $$\\" +
"\n $$ |" +
"\n$$$$$$$\\ $$$$$$\\ $$$$$$\\ $$$$$$\\ $$$$$$\\ $$$$$$\\" +
"\n$$ __$$\\ $$ __$$\\\\_$$ _| $$ __$$\\ $$ __$$\\ $$ __$$\\" +
"\n$$ | $$ |$$$$$$$$ | $$ | $$ | \\__|$$$$$$$$ |$$ / $$ |" +
"\n$$ | $$ |$$ ____| $$ |$$\\ $$ | $$ ____|$$ | $$ |" +
"\n$$ | $$ |\\$$$$$$$\\ \\$$$$ |$$ | \\$$$$$$$\\ \\$$$$$$$ |" +
"\n\__| \\__| \\_______| \\____/ \\__| v1.0 \\_______| \\____$$ |" +
"\n $$\\ $$ |" +
"\n kwnow softs on your network machines \\$$$$$$ |" +
"\n \\______/")
print()
print("Ce script Python a été développé dans le cadre d'un stage" +
"\nau groupe Hospi Grand Ouest (HGO). Il est toutefois adaptable." +
"\n\nIl a pour but de rechercher, et énumérer les logiciels installés" +
"\nsur les différents postes Windows raccordés au même réseau." +
"\n\nDéveloppé par Emmanuel Hery - linkedin.com/in/emmanuel-hery" + "\n" + "-"*60)
impt("s"); impt("m")
print("\n"*2)
with open("ADlist.txt") as f:
for hostname in f:
try:
mGet(hostname[:-1])
except WindowsError as e:
print(str(e) + " : " + hostname[:-1])
except:
print("Scan échoué pour le poste " + hostname[:-1])
search("Office")
#sh_sList("computerName")
if sList:
expt(sList)
if mList:
expt(mList)
input("Script terminé, appuyez sur Entrée pour quitter") |
Partager