Skip to content

Commit 74e2220

Browse files
authored
Create logfile-anonymiser.py
1 parent 441f132 commit 74e2220

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

logfile-anonymiser.py

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#!/usr/bin/env python2
2+
3+
import argparse
4+
import re
5+
import string
6+
import random
7+
import gzip
8+
9+
10+
# borrowed from http://stackoverflow.com/questions/2257441/ddg#2257449
11+
def id_generator(size=6, chars=string.ascii_lowercase + string.digits):
12+
return ''.join(random.choice(chars) for _ in range(size))
13+
14+
15+
if __name__ == "__main__":
16+
17+
argparser = argparse.ArgumentParser(description="Any Logfile Anonymiser, Replaces regex with uniq random characters")
18+
argparser.add_argument("-f", "--source-file", action="store", required="true", help="Input File")
19+
argparser.add_argument("-d", "--destination-file", action="store", required="true", help="Anonymised data output")
20+
argparser.add_argument("-r", "--replace-regex", action="store", required="true", help="Regex of source to replace")
21+
argparser.add_argument("-z", "--gzip", action="store_true", help="")
22+
args = argparser.parse_args()
23+
24+
if args.gzip:
25+
print("Opening as GZIP")
26+
source = gzip.open(args.source_file, "r")
27+
destination = gzip.open(args.destination_file, "w")
28+
else:
29+
print("Opening as normal")
30+
source = open(args.source_file, "r")
31+
destination = open(args.destination_file, "w")
32+
replacements = {}
33+
34+
for line in source:
35+
line = str(line)
36+
split = re.findall('{}'.format(args.replace_regex), line)
37+
if len(split) == 1:
38+
split = split[0]
39+
# print(split)
40+
if split:
41+
# "line" contains our pattern, and split contains
42+
if split in replacements:
43+
replace = replacements[split]
44+
else:
45+
replace = id_generator(len(split))
46+
replace = "/{}".format(replace[1:])
47+
replacements[split] = replace
48+
49+
newline = line.replace(split, replace)
50+
else:
51+
newline = line
52+
destination.write(newline)
53+
source.close()
54+
destination.close()

0 commit comments

Comments
 (0)