Validating an ip address in a bash script

posted by | Leave a comment

I have tried using grep from a temp file, sed from a temp file, sed from command line, comparison in an if condition and I cannot figure it out and I don't have much hair left! Thanks for your help OK I have tried the following expressions that according to the regex tool match IP addresses, but they all seem to flip flop back and forth for valid and not valid IPs and the entire list albeit 2 or 3 are valid. I would gladly do this another way if anyone could steer me in the right direction. rc=`egrep -c "[0-9][13]\.[0-9][13]\.[0-9][13]\.[0-9][13]"` rc=`egrep -c \b\(? \)\b"` rc=`egrep -c '\b\(25[0-5]|2[0-4][0-9]|[01]? In line 1 I'm simply replacing all numbers with , so that all that remains are the three points. I know you could enter 999.999.999.999 and it wouldn't be a valid ip address, but that's OK for me as long as nobody is able to slip in a "rm -R /". I think like many others I've fallen into the pitfall of trying to do this with a regular expression; while this works well enough for IPv4, IPv6 is more complex (as it supports compression of zeroes) and it's just a complicated and not especially readable solution.However, unix and linux clearly understand IP addresses well enough, so I'm wondering if there is a better way that I can validate IP addresses in bash? without checking or replying to that he posted again here.... \)\b'Moderator, Please close this thread, the same user posted the same question yesterday in shell programming section and I have replied for that...

function valid_ip() { local IPA1=

function valid_ip() { local IPA1=$1 local stat=1 if $ -le 255 && $ -le 255 \ && $ -le 255 && $ -le 255 # It's testing if any part of IP is more than 255 stat=$?

/bin/bash read -p " ip: " req_ipadr # ip_full=$(echo $req_ipadr | sed -n 's/^\(\(\([1-9][0-9]\?

\|[1][0-9]\\|[2][0-4][0-9]\|[2][5][0-4]\)\.\)\\([1-9][0-9]\? \|[1][0-9]\\|[2][0-4][0-9]\|[2][5][0-4]\)\)$/\1/p') # [ "$ip_full" !

I am very new to bash script so basically i can not understand it very much so please can any one suggest me ways that i can learn faster.

I am tryong to write a bash script to read ip adress and validate it.

||

function valid_ip() { local IPA1=$1 local stat=1 if $IPA1 =~ ^[0-9]\.[0-9]\.[0-9]\.[0-9]$ ; then OIFS=$IFS IFS='.' #read man, you will understand, this is internal field separator; which is set as '.' ip=($ip) # IP value is saved as array IFS=$OIFS #setting IFS back to its original value; $ -le 255 && $ -le 255 \ && $ -le 255 && $ -le 255 # It's testing if any part of IP is more than 255 stat=$?/bin/bash read -p " ip: " req_ipadr # ip_full=$(echo $req_ipadr | sed -n 's/^\(\(\([1-9][0-9]\?\|[1][0-9]\\|[2][0-4][0-9]\|[2][5][0-4]\)\.\)\\([1-9][0-9]\? \|[1][0-9]\\|[2][0-4][0-9]\|[2][5][0-4]\)\)$/\1/p') # [ "$ip_full" ! I am very new to bash script so basically i can not understand it very much so please can any one suggest me ways that i can learn faster.I am tryong to write a bash script to read ip adress and validate it.

]]

Leave a Reply

Hot chat lines always free