在服务器上的设置文件中查找硬编码的 IP 地址

发布于 2024-12-21 20:39:47 字数 258 浏览 3 评论 0原文

在准备多个服务器的重新 IP 时,我打算搜索设置文件中出现的服务器上的所有 IP 地址。

一个简单的第一次尝试:

grep -e "[0-9]*\\.[0-9]*\\.[0-9]"

..产生误报,因为没有数字的字符串也是允许的。此外,它还发现 1.2.3.4.5 等版本号不是有效的 IP 地址。

我确信有人已经考虑过这个问题,并想出了完美的 IP 地址查找正则表达式 grep 来涵盖所有例外情况。

In preparation for a re-IP of several servers I intend to search for all IP addresses on the servers which occur in settings files.

A simpleminded first attempt:

grep -e "[0-9]*\\.[0-9]*\\.[0-9]"

..produces false positives because strings with no digits is also allowed. Also, it finds version numbers like 1.2.3.4.5 which are not valid IP addresses.

I'm sure that someone has thought about this and come up with the perfect IP address finding regular expression grep that covers all exceptions.

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

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

发布评论

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

评论(1

旧人哭 2024-12-28 20:39:47

怎么样:

(^|[^\.0-9])([0-2]?[0-9]{,2}\.){3}[0-2]?[0-9]{,2}($|[^\.0-9])

使用 -e "extended" grep 运行。它不会匹配任何重复组数超过 4 个或大于 299 的数字(最好排除大于 255 的数字)。

显然,这仅适用于 IPv4 地址...

编辑:

构建于 这个问题的答案,这是一个仅匹配有效 IPv4 地址的版本(我对其进行了修改,因此它不会匹配超过四个组的数字):

(^|[^\.0-9])(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])($|[^\.0-9])

How about this:

(^|[^\.0-9])([0-2]?[0-9]{,2}\.){3}[0-2]?[0-9]{,2}($|[^\.0-9])

Run with -e "extended" grep. It won't match any numbers with more than four repeated groups, or that are greater than 299 (next best to excluding greater than 255).

Obviously, this only works for IPv4 adresses...

EDIT:

Building on this question's answer, here's a version that only matches valid IPv4 addresses (I modified it so it won't match numbers with more than four groups):

(^|[^\.0-9])(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])($|[^\.0-9])
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文