0
#!/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

Hello everyone. I am trying to validate IPv6 addresses using this script. It is working well. The problem is that it's also accepting IPs like 1111:2222:3333:4444::. Could you help me to avoid this types of IPs?

Andrea Corbellini
  • 17,339
  • 3
  • 53
  • 69
Manu Cherian
  • 111
  • 2

2 Answers2

0

Why don't you use built-in linux utility,
ipcalc --ipv6 1111:2222:3333:4444::
If it returns nothing then you've provided correct IP
If you provide incorrect IP it returns something like,
ipcalc: bad IPv6 address:

Hope it helps.

Mahadev Patil
  • 109
  • 11
-1

This regular expression should do the trick :

([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}

It matches 8 blocks of 1 to 4 hexadecimal values, joined by 7 :.

Aaron
  • 24,009
  • 2
  • 33
  • 57