apache-logfilter 591 B

12345678910111213141516171819202122232425262728293031
  1. #!/usr/bin/python3
  2. import argparse
  3. import pathlib
  4. import re
  5. import sys
  6. re_pw = re.compile(r'(password|pw|pass|passw|passwd)=[^& \t\n]*')
  7. def filterLine(line):
  8. line, n = re_pw.subn(r'\1=***', line, 0)
  9. return line
  10. def main():
  11. p = argparse.ArgumentParser()
  12. p.add_argument("output", type=pathlib.Path,
  13. help="Output log file.")
  14. args = p.parse_args()
  15. with open(args.output, "ab") as outf:
  16. while True:
  17. line = sys.stdin.readline()
  18. if not line:
  19. break
  20. line = filterLine(line)
  21. outf.write(line.encode("UTF-8", "ignore"))
  22. outf.flush()
  23. return 0
  24. sys.exit(main())