在 Web 分析工具(perl 或 sed)的输出中使 IP 匿名?

发布于 2024-12-22 01:33:58 字数 383 浏览 2 评论 0原文

我使用 http://www.w3perl.com/ 分析了日志,现在我希望 IP 匿名。

所以我希望它们在整个目录中的所有 html 文件中进行更改。

  1. 我考虑过将所有 IP 的第三个和第四个八位字节始终转换为相同的数字。但我读到了一些关于对它们进行异或运算的内容 - 那是关于什么的?

  2. 管理更换的更好方法是什么?为什么? sed 或 perl 或其他选项?

我对此很陌生,我希望我对其他主题进行了足够的搜索。我还没有找到任何与我的问题相关的内容。 预先感谢,请原谅我的英语。 这个网站太棒了——过去几天我学到了很多东西!

I analysed logs with http://www.w3perl.com/, now i want the IPs made anonymous.

So i want them changed in all html-files in the whole directory.

  1. I thought about just always turning the third and fourth octet into the same number for all IPs. But i read something about XOR-ing them - what is that about?

  2. what is the better way to manage the replacing and why? sed or perl or another option?

I am pretty new to this, i hope i searched the other topics enough. I haven't found anything related to my problem.
Thanks in advance and please excuse my English.
This website is just great - i've learned so much in the last days!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

救赎№ 2024-12-29 01:33:58

使用 perl 可以更好地实现 IP 地址屏蔽。您可以在此处查看如何使用已有的模块,这样您就不必重新发明轮子。不幸的是我不懂perl,所以我只能提供sed解决方案。

您可以使用类似的方法来替换文件中的所有 IP 地址。

sed 's/\([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\}/**masked ip**/g' < yourfile > newfile

您的文件:

[jaypal:~/Temp] cat fmp
this is 12.34.55.222 and 
21.3.34.5
also there is 133.42.55.23

第 1 步:识别 IP 地址:

[jaypal:~/Temp] sed 's/\([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\}/**&**/g' fmp
this is **12.34.55.222** and 
**21.3.34.5**
also there is **133.42.55.23**

第 2 步:用您的掩码替换 IP 地址

[jaypal:~/Temp] sed 's/\([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\}/**&**/g' fmp | sed '/\*\*.*\\*\*/s//**masked ip**/'
this is **masked ip** and 
**masked ip**
also there is **masked ip**

您可以使用 sed -i (内联) 替换或使用 > 将输出重定向到另一个文件。


显然,这可以简化为以下内容。上面的内容对于调试很有用,因为您可以在进行更改之前查看正在更改的内容。

[jaypal:~/Temp] sed 's/\([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\}/** masked ip **/g' fmp
this is ** masked ip ** and 
** masked ip **
also there is ** masked ip **

祝你好运!!

更新:

正如Mark评论中发布的那样,perl 变体将是 -

perl -pe 's/\([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\}/** masked ip **/g' < inputfile > outputfile

更新 II:

根据OP的要求,以下一行将找到文件并执行替换。

find /path/to/dir/ -type f -name "*.html" -exec perl -ibak -pe 's/\([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\}/** masked ip **/g' {} \;

Masking IP-Address can be better achieved with perl. You can take a look here for using already available modules so that you won't have to re-invent the wheel. Unfortunately I don't know perl, so I can only offer sed solution.

You can use something like this for replacing all ip-address in your file.

sed 's/\([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\}/**masked ip**/g' < yourfile > newfile

Your file:

[jaypal:~/Temp] cat fmp
this is 12.34.55.222 and 
21.3.34.5
also there is 133.42.55.23

Step 1: Identify IP-Addresses:

[jaypal:~/Temp] sed 's/\([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\}/**&**/g' fmp
this is **12.34.55.222** and 
**21.3.34.5**
also there is **133.42.55.23**

Step 2: Replace IP-Address with your mask

[jaypal:~/Temp] sed 's/\([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\}/**&**/g' fmp | sed '/\*\*.*\\*\*/s//**masked ip**/'
this is **masked ip** and 
**masked ip**
also there is **masked ip**

You can either make sed -i (in-line) substitution or use > to re-direct your output to another file.


Obviously this can be reduced to the following. The above would be useful for debugging as you can view what is being changed before making the change.

[jaypal:~/Temp] sed 's/\([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\}/** masked ip **/g' fmp
this is ** masked ip ** and 
** masked ip **
also there is ** masked ip **

Good Luck!!

Update:

As posted by Mark in the comments, a perl variant would be -

perl -pe 's/\([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\}/** masked ip **/g' < inputfile > outputfile

Update II:

As requested by OP, the following one-liner will find the files and perform the substitution.

find /path/to/dir/ -type f -name "*.html" -exec perl -ibak -pe 's/\([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\}/** masked ip **/g' {} \;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文