lidia
lidia

Reputation: 3183

shell script + short syntax to verify valid IP

my question is , I want to get some short and smart ideas to verify the IP address then my example

Maybe some perl syntax that I can combine in my ksh script

lidia

Upvotes: 1

Views: 5914

Answers (6)

Gopu
Gopu

Reputation: 1032

save the ip address to ip_server and check it using the below code :

if [[ "$ip_server" =~ ^([0-9]{1,3})[.]([0-9]{1,3})[.]([0-9]{1,3})[.]([0-9]{1,3})$ ]]
then
    for (( i=1; i<${#BASH_REMATCH[@]}; ++i ))
    do
      (( ${BASH_REMATCH[$i]} &2; exit 1; }
    done
else
      echo "Wrong IP address" >&2
      exit 1;
fi

Upvotes: 0

David Avikasis
David Avikasis

Reputation: 636

Here is some shorter and simpler way to do it. It only checks for the basic structure but for some cases it's enough.

VALID=$(echo $IP | egrep '^[[:digit:]]{1,3}\.[[:digit:]]{1,3}\.[[:digit:]]{1,3}\.[[:digit:]]{1,3}$');

if [ ! -n "$VALID" ]; then
   echo "This IP ($IP) isn't valid. Please check it and try again.";
   exit 0;
fi;

Upvotes: 2

Fritz G. Mehner
Fritz G. Mehner

Reputation: 17188

Split the address without touching IFS and avoid complicated checks by bitwise shift:

declare -a part=( ${ip//\./ } )
declare -i valid=0

for p in ${part[@]}; do
  if [[ $p =~ ^[[:digit:]]+$ ]] ; then
    ((valid += p>>8 ))
  else
    ((valid++))
  fi
done

if [ $valid -eq 0 ] ; then
  echo -e "$ip     OK"
else
  echo -e "$ip NOT OK"
fi

Upvotes: 1

ghostdog74
ghostdog74

Reputation: 342363

check(){
  case "$1" in
     [0-9]|[0-9][0-9]|[0-1][0-9][0-9]|2[0-4][0-9]|25[0-5] ) echo "0";;
     *) echo "1";;
  esac
}
ip="$1"
OIFS="$IFS"
IFS="."
set -- $ip
result="$(check $1)$(check $2)$(check $3)$(check $4)"
case "$result" in
  "0000" ) echo "IP $ip Ok";;
  *) echo "IP $ip not ok";;
esac
IFS="$OLDIFS"

Upvotes: 1

Dennis Williamson
Dennis Williamson

Reputation: 360085

Bash >= version 3.2 (this could be shortened up considerably):

valid () {
    if [[ $1 =~ ^[[:digit:]]+$ ]] && 
       (( $1 >= 0 && $1 <= 255 ))
    then
        echo "0"
        return 0
    else
        echo "1"
        return 1
    fi
}

saveIFS=$IFS
IFS='.'
ip=($1)
IFS=$saveIFS
for octet in ${ip[@]}
do
    if ! valid $octet > /dev/null
    then
        valid=1
    fi
done
[[ $valid != 1 ]] && echo "Good address" || echo "Bad address"

Upvotes: 0

mfontani
mfontani

Reputation: 2964

Don't reinvent the wheel.

use strict;
use warnings;
use Regexp::Common qw/net/;
# see http://search.cpan.org/dist/Regexp-Common/lib/Regexp/Common/net.pm

my $Address = '...';
# adapted from the module's synopsis
for ( $Address ) { 
    /$RE{net}{IPv4}/       and print "Dotted decimal IP address";
    /$RE{net}{IPv4}{hex}/  and print "Dotted hexadecimal IP address";
    /$RE{net}{IPv4}{oct}{-sep => ':'}/ and
                           print "Colon separated octal IP address";
    /$RE{net}{IPv4}{bin}/  and print "Dotted binary IP address";
    /$RE{net}{MAC}/        and print "MAC address";
    /$RE{net}{MAC}{oct}{-sep => " "}/ and
                           print "Space separated octal MAC address";
}

Use the one you need.

If you cannot install the module, then just lurk through the module's code and get the correct regexp to use, depending on what kind of IP address you'd like to match.

Or, just use something like the above and call the same sub if the address matches any of the notations you want, or something along those lines.

Using it from your shell script would be along the lines of:

return perl -e'use Regexp::Common qw/net/;$ip=shift;if ($ip =~ /$RE{net}{IPv4}/){exit 0}else{exit 1}' "$Address";

The above would replace your complete "case" block.

Again, if you need to inline the regex in the perl script call you can do so by reading the module's code.

Upvotes: 1

Related Questions