Skip to content

Instantly share code, notes, and snippets.

@xai
Created March 13, 2017 16:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xai/9f45c65fe9653fa59047cd9a97581bc1 to your computer and use it in GitHub Desktop.
Save xai/9f45c65fe9653fa59047cd9a97581bc1 to your computer and use it in GitHub Desktop.
Anonymize logfiles
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# vim:fenc=utf-8
#
# Copyright © 2017 Olaf Lessenich <xai@linux.com>
#
# Distributed under terms of the MIT license.
"""
Anonymize logfiles
"""
import re
import sys
def anon(s):
return ('[ANONYMIZED %s]' % s.upper())
regexes = dict()
regexes['mail'] = re.compile(r"([a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+)")
regexes['ip'] = re.compile(r"(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})")
for line in sys.stdin:
for name, regex in regexes.items():
line = regex.sub(anon(name), line)
sys.stdout.write(line)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment