Posted on 2005-11-11 16:40:20-08 by jtk
Sample script to anonymize all IPv4 address looking text in log files

So, say you have some logs in a file called logfile that contain IP addresses and you'd like to anonymize them and be able to publish the anonymized version of the logs. You could do something like this:

./anonymize-logs logfile

Where anonymize-logs looks something like this (be sure to change the 32 values in @key to random values between 0 to 255, this becomes your private key):

#!/usr/bin/perl -wT use strict; $|=1; use IP::Anonymous; my @key = (0,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); my $obj = new IP::Anonymous(@key); while(defined(my $line=<>)) { chomp $line; if($line =~ /\d{1,3}(?:\.\d{1,3}){3}/) { $line =~ s/(\d{1,3}(?:\.\d{1,3}){3})/$obj->anonymize($1)/eg; } print $line."\n"; }
John
Direct Responses: Write a response
Perl Weekly newsletter
A free weekly newsletter for people who are busy to read all the blogs. click here to check it out.