ip保留地址检查功能

发布于 2024-12-12 09:18:30 字数 230 浏览 0 评论 0原文

我有一个 IP 地址列表。我想知道其中是否有一个是保留地址(参考:http://en.wikipedia.org/ wiki/Reserved_IP_addresses )。对于我的作品,我使用 awk,所以我想知道是否有一些预制的 shell 脚本或类似的脚本可以帮助我识别此类地址。

I have a list of ip adrresses. I want to know if any of them is a reserved address (ref: http://en.wikipedia.org/wiki/Reserved_IP_addresses ). For my works I'm using awk so I'm wondering if there are some pre-made shell scripts or similar that could help me in identifyng such addresses.

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

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

发布评论

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

评论(2

醉生梦死 2024-12-19 09:18:30

你知道 unix grep 吗?

您可以使用 grep 搜索存储在某个文件或其他文件中的目标字符串列表。

 fgrep -f reservedList.txt yourListOfIpsSavedToAfile

将打印yourList*中与reservedList中找到的任何单词匹配的任何行。

如果您在 shell 脚本内执行此操作,则可以使操作成为有条件的。

 if fgrep -f reservedList.txt yourList > /dev/null 2>&1 ; then
    echo "found items in reserved List"
 else
    echo "no items found in reserved List"
 fi

您可能需要使用 grep -FservedList ....。使用上面的第一个示例来查看它是否有效,如果没有,请查阅 man grep 来查看在您的 OS/grep 版本中使用哪些确切参数。

如果您确实需要在 awk 中执行此操作,那么您基本上将在 awk 中重新创建此功能。

我希望这有帮助。

Do you know about unix grep ?

You can have grep search for a list of target strings stored in a file, in your other files.

 fgrep -f reservedList.txt yourListOfIpsSavedToAfile

will print any lines in yourList* that matches any word found in reservedList.

If you are doing this inside a shell script, then you can make your actions conditional

 if fgrep -f reservedList.txt yourList > /dev/null 2>&1 ; then
    echo "found items in reserved List"
 else
    echo "no items found in reserved List"
 fi

You may need to use grep -F reservedList ..... Use the first example above to see that is working, and if not consult man grep to see which exact arguments to use in your OS/grep versions.

If you really need to do this in awk, you'll basically be remaking this functionality in awk.

I hope this helps.

你的背包 2024-12-19 09:18:30

如果您逐行将良好的旧 IPv4 地址列表作为脚本的输入

while IFS=. read A B C D
do  ADDR=`printf %03d.%03d.%03d.%03d $A $B $C $D`
    [[ 000.255.255.255 < $ADDR && $ADDR < 010.000.000.000 ]] && continue
    [[ 010.255.255.255 < $ADDR && $ADDR < 100.064.000.000 ]] && continue
    [[ 100.127.255.255 < $ADDR && $ADDR < 127.000.000.000 ]] && continue
    [[ 127.255.255.255 < $ADDR && $ADDR < 169.254.000.000 ]] && continue
    [[ 169.254.255.255 < $ADDR && $ADDR < 172.016.000.000 ]] && continue
    [[ 172.031.255.255 < $ADDR && $ADDR < 192.000.000.000 ]] && continue
    [[ 192.000.000.007 < $ADDR && $ADDR < 192.000.002.000 ]] && continue
    [[ 192.000.002.255 < $ADDR && $ADDR < 192.088.099.000 ]] && continue
    [[ 192.088.099.255 < $ADDR && $ADDR < 192.168.000.000 ]] && continue
    [[ 192.168.255.255 < $ADDR && $ADDR < 198.018.000.000 ]] && continue
    [[ 198.019.255.255 < $ADDR && $ADDR < 198.051.100.000 ]] && continue
    [[ 198.051.100.255 < $ADDR && $ADDR < 203.000.113.000 ]] && continue
    [[ 203.000.113.255 < $ADDR && $ADDR < 224.000.000.000 ]] && continue
    echo $A.$B.$C.$D is reserved
done

,它将输出所有保留的地址。

If you feed your list of good old IPv4 addresses line by line as input to the script

while IFS=. read A B C D
do  ADDR=`printf %03d.%03d.%03d.%03d $A $B $C $D`
    [[ 000.255.255.255 < $ADDR && $ADDR < 010.000.000.000 ]] && continue
    [[ 010.255.255.255 < $ADDR && $ADDR < 100.064.000.000 ]] && continue
    [[ 100.127.255.255 < $ADDR && $ADDR < 127.000.000.000 ]] && continue
    [[ 127.255.255.255 < $ADDR && $ADDR < 169.254.000.000 ]] && continue
    [[ 169.254.255.255 < $ADDR && $ADDR < 172.016.000.000 ]] && continue
    [[ 172.031.255.255 < $ADDR && $ADDR < 192.000.000.000 ]] && continue
    [[ 192.000.000.007 < $ADDR && $ADDR < 192.000.002.000 ]] && continue
    [[ 192.000.002.255 < $ADDR && $ADDR < 192.088.099.000 ]] && continue
    [[ 192.088.099.255 < $ADDR && $ADDR < 192.168.000.000 ]] && continue
    [[ 192.168.255.255 < $ADDR && $ADDR < 198.018.000.000 ]] && continue
    [[ 198.019.255.255 < $ADDR && $ADDR < 198.051.100.000 ]] && continue
    [[ 198.051.100.255 < $ADDR && $ADDR < 203.000.113.000 ]] && continue
    [[ 203.000.113.255 < $ADDR && $ADDR < 224.000.000.000 ]] && continue
    echo $A.$B.$C.$D is reserved
done

, it will output any reserved addresses.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文