3

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?

5
  • good..can you explain expressions between '' ? thank you Commented Feb 13, 2014 at 7:01
  • you mean ? right? or something else? Commented Feb 13, 2014 at 7:14
  • i think ^ means beginning of ip addr, | means OR, but don't know what it does... and ? as well...also 25[0-4]...thank you Commented Feb 13, 2014 at 7:16
  • i think 25[0-4] means 250-251-252-253-254 Commented Feb 13, 2014 at 7:18
  • @MortezaLSC yes correct Commented Feb 13, 2014 at 7:29

2 Answers 2

7

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"
}
Sign up to request clarification or add additional context in comments.

Comments

0

mine:

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
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.