Validating an ip address in a bash script

posted by | Leave a comment

TEST_PASSES=$(( TEST_PASSES 1 )) done for TEST_IP in ::1 ::1/128 ::1/0 ::1234 ::bad ::12 1:2:3:4:5:6:7:8 12ab:cdef:12ab:cdef \ 12ab:cdef:12ab:cdef/127 12ab::56ab:cdef/64 f:1234:c:ba:240::1 \ 1:2:3:4:5:6:1.2.3.4 ::1.2.3.4 ::1.2.3.4/0 ::ffff:1.2.3.4 ;do !is_ip6 "$TEST_IP" && printf "IP6 test failed, test case '%s' returned invalid\n" "$TEST_IP" && TEST_FAILURES=$(( TEST_FAILURES 1 )) ; do read -p "Wrong name format. "$ip" =~ '^((25[0-5]|2[0-4][0-9]|[01][0-9][0-9]|[0-9])[.])(25[0-5]|2[0-4][0-9]|[01][0-9][0-9]|[0-9])$' ]]; do read -p "Not an IP. /bin/sh echo "Enter a name:" read FULL_NAME FULL_NAME_REPLACED="$(echo $FULL_NAME | tr ' ' _)" echo $FULL_NAME_REPLACED echo "Enter an IP address:" read IP_ADDRESS if echo "$IP_ADDRESS" | egrep -E '[0-9]\.[0-9]\.[0-9]\.[0-9]' then # Then the format looks right - check that each octect is less # than or equal to 255: VALID_IP_ADDRESS="$(echo $IP_ADDRESS | awk -F'.' '$1 unset name ip; \ while ! [ "$ip" ];do printf "Name: %s\r" $name; read -p Name:\ var; [ "$var" ] && name=$; printf "IP: %s\r" $ip; read -p IP:\ var; iparray=($( IFS=".";echo $var;)); [ $ -eq 4 ] && \ [ $iparray -ge 0 ] && [ $iparray -le 255 ] && \ [ $ -ge 0 ] && [ $ -le 255 ] && \ [ $ -ge 0 ] && [ $ -le 255 ] && \ [ $ -ge 0 ] && [ $ -le 255 ] && \ ip=$var; [ "$name" ] && [ "$ip" ] || echo something wrong...; done; \ printf "Name: '%s'\n IP: '%s'\n" $name $ip read name name=`echo $name|tr ' ' _` read ip stat=1 if $ip =~ ^[0-9]\.[0-9]\.[0-9]\.[0-9]$ ; then OIFS=$IFS IFS='.' ip=($ip) IFS=$OIFS $ -le 255 && $ -le 255 \ && $ -le 255 && $ -le 255 stat=$?Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers. 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=$?#If any part of IP as tested above is more than 255 stat will have a non zero value fi return $stat # as expected returning I have had issues finding a good answer to this, as well, but I finally came up with a line that properly validates if it is a real IP or not (format-wise).You could probably find a library for a common scripting language which properly encapsulates this logic in a library.

||

is_ip4 "$TEST_IP" && printf "IP4 test failed, test case '%s' returned invalid\n" "$TEST_IP" && TEST_FAILURES=$(( TEST_FAILURES 1 )) || TEST_PASSES=$(( TEST_PASSES 1 )) done for TEST_IP in ::1 ::1/128 ::1/0 ::1234 ::bad ::12 1:2:3:4:5:6:7:8 12ab:cdef:12ab:cdef \ 12ab:cdef:12ab:cdef/127 12ab::56ab:cdef/64 f:1234:c:ba:240::1 \ 1:2:3:4:5:6:1.2.3.4 ::1.2.3.4 ::1.2.3.4/0 ::ffff:1.2.3.4 ;do !

is_ip6 "$TEST_IP" && printf "IP6 test failed, test case '%s' returned invalid\n" "$TEST_IP" && TEST_FAILURES=$(( TEST_FAILURES 1 )) || TEST_PASSES=$(( TEST_PASSES 1 )) done for TEST_IP in junk .

"$name" =~ '[A-Za-z ]' ]]; do read -p "Wrong name format. "$ip" =~ '^((25[0-5]|2[0-4][0-9]|[01][0-9][0-9]|[0-9])[.])(25[0-5]|2[0-4][0-9]|[01][0-9][0-9]|[0-9])$' ]]; do read -p "Not an IP. /bin/sh echo "Enter a name:" read FULL_NAME FULL_NAME_REPLACED="$(echo $FULL_NAME | tr ' ' _)" echo $FULL_NAME_REPLACED echo "Enter an IP address:" read IP_ADDRESS if echo "$IP_ADDRESS" | egrep -E '[0-9]\.[0-9]\.[0-9]\.[0-9]' then # Then the format looks right - check that each octect is less # than or equal to 255: VALID_IP_ADDRESS="$(echo $IP_ADDRESS | awk -F'.' '$1 unset name ip; \ while ! [ "$ip" ];do printf "Name: %s\r" $name; read -p Name:\ var; [ "$var" ] && name=$; printf "IP: %s\r" $ip; read -p IP:\ var; iparray=($( IFS=".";echo $var;)); [ $ -eq 4 ] && \ [ $iparray -ge 0 ] && [ $iparray -le 255 ] && \ [ $ -ge 0 ] && [ $ -le 255 ] && \ [ $ -ge 0 ] && [ $ -le 255 ] && \ [ $ -ge 0 ] && [ $ -le 255 ] && \ ip=$var; [ "$name" ] && [ "$ip" ] || echo something wrong...; done; \ printf "Name: '%s'\n IP: '%s'\n" $name $ip read name name=`echo $name|tr ' ' _` read ip stat=1 if $ip =~ ^[0-9]\.[0-9]\.[0-9]\.[0-9]$ ; then OIFS=$IFS IFS='.' ip=($ip) IFS=$OIFS $ -le 255 && $ -le 255 \ && $ -le 255 && $ -le 255 stat=$?

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

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=$?

#If any part of IP as tested above is more than 255 stat will have a non zero value fi return $stat # as expected returning I have had issues finding a good answer to this, as well, but I finally came up with a line that properly validates if it is a real IP or not (format-wise).

You could probably find a library for a common scripting language which properly encapsulates this logic in a library.

My thoughts would go to Python, where indeed Python 3.3 includes a standard module called have a subnet mask just pass it a dummy one when performing the test.

]]
validating an ip address in a bash script-8

The other is expand ipv6 address in shell script, this is simple, but for major distribution of Linux, sipcalc isn't a common default utility.So my question, is there a simple way or utility to validate a IPv6 address with shell? The code in the first link isn't particularly elegant, but modulo stylistic fixes, I don't think you can simplify much beyond that (and as indicated in a comment, it may already be too simple).The spec is complex and mandates a number of optional features, which is nice for the end user, but cumbersome for the implementor.Visit Stack Exchange Quite simply I'd like to verify that a string represents a valid IP address in a bash script.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.

Leave a Reply

Free live sex webcams qld