开发者

How to censor IP addresses in a file with Python?

I have a log file containing some Whois entries with relative IP addresses which I want to censor like: 81.190.123.123 in 81.190.xxx.xxx.

Is there a way to make such a conversion and rewrite the file contents without 开发者_开发知识库modifying the rest?

Thank you for the help!


As mentioned above, you can do this with sed:

sed -E -e 's/([0-9]+\.[0-9]+)\.[0-9]+\.[0-9]+/\1.xxx.xxx/g'

This uses a regular expression match to look for IP addresses and replace the last two octets with xxx. Using the -i switch, you can do this all at once:

sed -i.bak -E -e 's/([0-9]+\.[0-9]+)\.[0-9]+\.[0-9]+/\1.xxx.xxx/g' file.txt


If you do want to use Python then use the fileinput module to process a file or files line by line.

import fileinput
for line in fileinput.input(["filename"], inplace=1, backup='.bak'):
    print processed(line)
fileinput.close()

fileinput with the inplace=1 will rename the input file and read from the renamed file while directing stdout to a new file of the same name. You can use the backup parameter to prevent the temporary file being deleted automatically.

If the input is important you'll need to take care to handle exceptions to prevent the input being lost in case of an error.


If Python is not actually one of your requirements, this also solves the problem:

sed -i 's/\([0-9]\{1,3\}\)\.\([0-9]\{1,3\}\)\.[0-9]\{1,3\}\.[0-9]\{1,3\}/\1.\2.xxx.xxx/g' mylogfile.log

Or Perl, which lets you get rid of most of the ugly backslashes:

perl -i -pe 's/(\d{1,3})\.(\d{1,3})\.\d{1,3}\.\d{1,3}/$1.$2.xxx.xxx/g' mylogfile.log

But this does not have the "inline" flag -i.


import re
ip = "123.456.789.123"
ip = ip[::-1]
ip = re.sub("([0-9]{1,3})", "xxx", ip, count=2)
ip = ip[::-1]
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜