linux - ipv6 validation using regex -


#!/bin/bash echo "enter 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]))$ ]];          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. trying validate ipv6 addresses using script. working well. problem it's accepting ips 1111:2222:3333:4444::. me avoid types of ips?

why don't use built-in linux utility,
ipcalc --ipv6 1111:2222:3333:4444::
if returns nothing you've provided correct ip
if provide incorrect ip returns like,
ipcalc: bad ipv6 address:

hope helps.


Comments

Popular posts from this blog

Ansible - ERROR! the field 'hosts' is required but was not set -

customize file_field button ruby on rails -

SoapUI on windows 10 - high DPI/4K scaling issue -