regex - 使用正则表达式进行 ipv6 验证

标签 regex linux ipv6

#!/bin/bash
echo "enter the ip address:"
read s
if [[ $s =~ ^(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))$ ]]; then

        echo -e '\E[47;31m'"\033[1mIPv6 Format\033[0m" 
        echo -n "The IPv6 Address Expanded Form:"
        EXPANDED=`sipcalc $s | fgrep Expand | cut -d '-' -f 2`
        echo -e "\033[32m $EXPANDED\033[0m"
        echo -n "IPv6 address Compress Form:"
        Compress=`sipcalc $s | fgrep Comp | cut -d '-' -f 2`
        echo  -e "\033[32m$Compress\033[0m"
        echo -n "Address Type of IPv6:"
        type=`sipcalc $s | fgrep type | cut -d '-'  -f 2,3,4`
        comment=`sipcalc $s | fgrep Comment | cut -d '-' -f 2`
        echo -e "\033[32m $type$comment\033[0m"
else
        echo  -e '\E[37;44m'"\033[1mNOT VALID IPv6 address\033[0m"
fi

大家好。我正在尝试使用此脚本验证 IPv6 地址。它运作良好。问题是它也接受像 1111:2222:3333:4444:: 这样的 IP。您能帮助我避免此类 IP 吗?

最佳答案

为什么不使用内置的 Linux 实用程序,
ipcalc --ipv6 1111:2222:3333:4444::
如果它没有返回任何内容,那么您就提供了正确的 IP
如果您提供的 IP 不正确,它会返回类似以下内容的内容:
ipcalc:错误的 IPv6 地址:

希望有帮助。

关于regex - 使用正则表达式进行 ipv6 验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36714230/

相关文章:

linux - 这个linux命令的结果是什么

IPv6数据包的UDP校验和计算

mysql - 如何在关系数据库中存储与 IPv6 兼容的地址

php - 如果 2 个 IPv6 地址相等,我如何检查 PHP?

javascript - 正则表达式匹配最后一个冒号后面不在大括号内的字符

ios - 如何排除 "+"在 NSRegularExpression 中被视为特殊字符?

linux - 如何从命令行在 *nix 中将文件显示为 "selected"?

PHP:如何获取所有正则表达式匹配项?

java - 将 Struts 2.1 升级到 Struts 2.3 时电子邮件验证面临问题

linux - java.lang.NoClassDefFoundError : com/tinkerpop/blueprints/impls/orient/OrientGraphFactory 错误