Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Netmask validation seems not working using regex in bash script while ip validation working fine

I wrote simple script to validate IP address and Netmask 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 above result i have successfully filter out invalid IPaddress but not able to filter invalid netmask.I have run above script with different argument as follows and see the output also below after script executing

$ ./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 output the result for IP address validation is expected but why it reject the netmask even i enter valid netmask `255.255.255.0 ?

Any one have idea what i miss in netmask validation or something wrong in my script?

like image 266
Jayesh Bhoi Avatar asked Jan 28 '26 11:01

Jayesh Bhoi


1 Answers

grep doesn't double escaping dots etc so this will work:

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
}

Better to use this concise version:

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"
}
like image 176
anubhava Avatar answered Jan 31 '26 05:01

anubhava



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!