- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChecksumCheck.py
More file actions
Latest commit
106 lines (94 loc) · 4.3 KB
/
Copy pathChecksumCheck.py
File metadata and controls
106 lines (94 loc) · 4.3 KB
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
importos
importhashlib
importsys
importjson
version="0.0.1"
author="@AdSanz_IT"
#dictionaries that will hold the file with his checksum
dict_hashes= {}
dict_hashes1= {}
#file where the hashes will be saved
filename='hash.txt'
#source admits a list of dicrectories, remember if the path has "\" it must be specified like C:\\System\\... Because in python backslash is an escape character ej -> "source = ['E:\\ASIR2\\ACTUALIZADO\\ASOE104','E:\\ASIR2\\ACTUALIZADO\\ASOE103']"
source= []
#source = ''
defhelp_s():
print("This script is intended to check the integrity of files located in the \"source\" recursively. It saves the checksums on {}, and later on you can compare them overtime, it also checks whether a new file was created.\n\nThe tool itself is to check whether a file has been modified by any means.\n\n Usage = \n ChecksumChecker.py 1/2 \n\n Options: \n--> 1 = Get checksums on source and save it on {} \n--> 2 = Compare the checksums on source with checksums saved on {} \n\nRemember: whenever you change the source variable, you should use (1) to save the checksum of those files before checking!!!".format(filename,filename,filename))
defget_checksums_list():
# Use this def if "source" is a list of dicrectories
foritemsinsource:
forsubdirs,dirs,filesinos.walk(items):
fornameinfiles:
filename1=os.path.join(subdirs,name)
withopen(filename1, 'rb') asf:
hasher=hashlib.md5()
whileTrue:
data=f.read(65536)
ifnotdata:
break
hasher.update(data)
hex_val=hasher.hexdigest()
dict_hashes.update({filename1:hex_val})
defget_checksums():
# Use this def is "source" is only a directory and not a list
forsubdirs,dirs,filesinos.walk(source):
fornameinfiles:
filename1=os.path.join(subdirs,name)
withopen(filename1, 'rb') asf:
hasher=hashlib.md5()
whileTrue:
data=f.read(65536)
ifnotdata:
break
hasher.update(data)
hex_val=hasher.hexdigest()
dict_hashes.update({filename1:hex_val})
defget_checksums1():
# This def get the checksums of the file and saves it on a dictionary
withopen(filename, 'r+') asoutput:
dict_hashes1.update(json.load(output))
defcreate():
# Save checksums (from get_checksums / get_checksums_list) on the file
withopen(filename, 'w+') aschecksums:
checksums.write(json.dumps(dict_hashes))
defcompare_checksums():
forkey,valueinzip(dict_hashes.keys(),dict_hashes.values()):
ifkeyindict_hashes1.keys():
ifvalueindict_hashes1.values():
print("Checked: {}".format(key))
print("Integrity OK")
elifvaluenotindict_hashes1.values():
print("Checked: {}".format(key))
print("New checksum -> \"{}\" | checksum on {} -> \"{}\"".format(value,filename,dict_hashes1[key]))
print("Integrity not OK")
elifkeynotindict_hashes1.keys():
print("The file {} is not on {}, not checking".format(key,filename))
pass
iflen(sys.argv) <2:
help_s()
eliflen(sys.argv) ==2andlen(sys.argv) <3:
print(" Script made by {}: https://github.com/adsanz/Python-scripts \n Version: {}\n\n".format(author,version))
ifsys.argv[1] =="1":
try:
get_checksums()
except:
get_checksums_list()
create()
ifos.path.isfile(filename):
print("Saved checksums on {}".format(filename))
else:
print("Created file {} and saved checksums".format(filename))
elifsys.argv[1] =="2":
try:
get_checksums()
except:
get_checksums_list()
get_checksums1()
compare_checksums()
print("Done...")
else:
print("Choose a value between 1 and 2\n")
help_s()
else:
print("This script only takes 1 argument between 1 and 2\n")
help_s()