IP-Scripting Issue

Hi Guys,

I am quite new to Shell Scripting... I need ur help.. This is very urgent.

The thing is like,
I need to match a IP address (ex 192.168.200.56) i.e, xxx.xxx.xxx.xx inside a KSH script,but if we enter in different format other than the specified format (ex jjj.ksj., 1.0...), it should throw an error.

Appreciate ur help...

Regards,

Mahesh Raghunandanan

Something like this:

$ echo "1.2.3.4" | grep -qE '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' ; echo $?
0
$ echo "1.2.3" | grep -qE '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' ; echo $?
1
$ echo "10.0.0.a" | grep -qE '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' ; echo $?
1

Check $? with an if and do what you need to.

Hi Pludi..

Can you just explain abt ur solution...

Regards,

Mahesh..

function check_ip_addr {
  typeset ipaddr=$1
  [[ $ipaddr = @(0|[1-9]?([0-9])?([0-9])).@(0|[1-9]?([0-9])?([0-9])).@(0|[1-9]?([0-9])?([0-9])).@(0|[1-9]?([0-9])?([0-9])) ]] || return 1
  typeset -i n1 n2 n3 n4
  print $ipaddr | IFS=. read n1 n2 n3 n4
  (( (n1 < 256) && (n2 < 256) && (n3 < 256) && (n4 < 256) )) || return 1
}

Not tested.