Mask authentication does not work using regex in bash script, and ip authentication works fine

I wrote a simple script to check the IP address and network mask as follows

#!/bin/bash

validFormatIP()
{
    echo $1 | grep -w -E -o '^(25[0-4]|2[0-4][0-9]|1[0-9][0-9]|[1]?[1-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)' > /dev/null
    if [ $? -eq 0 ]
    then
        echo "Valid ipaddress"
    else
        echo "Inalid ipaddress"
    fi
}

validNetMask()
{
    echo $1 | grep -w -E -o '^(254|252|248|240|224|192|128)\\.0\\.0\\.0|255\\.(254|252|248|240|224|192|128|0)\\.0\\.0|255\\.255\\.(254|252|248|240|224|192|128|0)\\.0|255\\.255\\.255\\.(254|252|248|240|224|192|128|0)' > /dev/null
    if [ $? -eq 0 ]
    then
        echo "Valid netmask"
    else
        echo "Invalid netmask"
    fi  
}

setIpAddress()
{
    ip_address=`echo $1 | awk -F= '{print $2}'` 
    validFormatIP $ip_address
}
setNetMask()
{
    ip_address=`echo $1 | awk -F= '{print $2}'` 
    validNetMask $ip_address
}

let "n_count=0"
netmask=""

let "i_count=0"
ipaddess=""

while getopts ":i:n:" OPTION
do
    case $OPTION in
        n)
            netmask="Netmask=$OPTARG"
            let "n_count+=1"
            ;;
        i)
            ipaddess="IpAddess=$OPTARG"
            let "i_count+=1"
            ;;

        ?)  
            echo "wrong command syntax"
            ;;
    esac
done

if [ $i_count -eq 1 ]
then
    setIpAddress $ipaddess
    exit 0
fi

if [ $n_count -eq 1 ]
then
    setNetMask $netmask
    exit 0
fi

Using the above result, I successfully filter the invalid IPdress, but cannot filter the invalid netmask. I skipped the above script with various arguments as follows and look at the output also below after the execution script

$ ./script.bash -i 192.168.0.1
Valid ipaddress

$./script.bash -i 255.255.0.0
Inalid ipaddress

$./script.bash -n 255.255.255.0
Invalid netmask

As you see above, the result for checking the IP address is expected, but why does it reject the netmask, even I enter a valid netmask `255.255.255.0?

Does anyone have an idea that I missed the network mask check or is there something wrong in my script?

+3
source share
2

grep .., :

validNetMask() {
   echo $1 | grep -w -E -o '^(254|252|248|240|224|192|128)\.0\.0\.0|255\.(254|252|248|240|224|192|128|0)\.0\.0|255\.255\.(254|252|248|240|224|192|128|0)\.0|255\.255\.255\.(254|252|248|240|224|192|128|0)' > /dev/null
   if [ $? -eq 0 ]; then
      echo "Valid netmask"
   else
      echo "Invalid netmask"
   fi
}

:

validNetMask() {
   grep -E -q '^(254|252|248|240|224|192|128)\.0\.0\.0|255\.(254|252|248|240|224|192|128|0)\.0\.0|255\.255\.(254|252|248|240|224|192|128|0)\.0|255\.255\.255\.(254|252|248|240|224|192|128|0)' <<< "$1" && echo "Valid netmask" || echo "Invalid netmask"
}
+5

:

validate_netmask () {
    n_masks=(${1//./ })
    [ "${#n_masks[@]}" -ne 4 ] && return 1
    for i in ${1//./ }; do
        bits=$(echo "obase=2;ibase=10;$i" | bc)
        pre=$((8-${#bits}))
        if [ "$bits" = 0 ]; then
            zeros=00000000
        elif [ "$pre" -gt 0 ]; then
            zeros=$(for ((i=1;i<=$pre;i++)); do echo -n 0; done)
        fi
        b_mask=$b_mask$zeros$bits
            unset zeros
    done
    if [ $b_mask = ${b_mask%%0*}${b_mask##*1} ]; then
        return 0
    else
        return 1
    fi
}
0

All Articles