Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check for IP validity

Tags:

linux

shell

How do I check the validity of an IP address in a shell script, that is within the range 0.0.0.0 to 255.255.255.255?

like image 856
AlwaysALearner Avatar asked Dec 08 '12 12:12

AlwaysALearner


People also ask

Which script correctly validates an IP address?

The FILTER_VALIDATE_IP filter validates an IP address. Possible flags: FILTER_FLAG_IPV4 - The value must be a valid IPv4 address. FILTER_FLAG_IPV6 - The value must be a valid IPv6 address.

How do you check IP address is valid or not in Java?

We can use InetAddressValidator class that provides the following validation methods to validate an IPv4 or IPv6 address. isValid(inetAddress) : Returns true if the specified string is a valid IPv4 or IPv6 address. isValidInet4Address(inet4Address) : Returns true if the specified string is a valid IPv4 address.

Is 0.0 0.0 is a valid IP?

0.0 as a reserved, special-purpose address for "this host, this network." Its IPv6 equivalent is expressed as ::/0. Although 0.0. 0.0 is valid address syntax, a client device using it as a source IP address cannot communicate on a network. IPv4 address numbers start with 0.0.


1 Answers

If you're using bash, you can do a simple regex match for the pattern, without validating the quads:

#!/usr/bin/env bash  ip=1.2.3.4  if [[ $ip =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$ ]]; then   echo "success" else   echo "fail" fi 

If you're stuck with a POSIX shell, then you can use expr to do basically the same thing, using BRE instead of ERE:

#!/bin/sh  ip=1.2.3.4  if expr "$ip" : '[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*$' >/dev/null; then   echo "success" else   echo "fail" fi 

Note that expr assumes that your regex is anchored to the left-hand-side of the string, so the initial ^ is unnecessary.

If it's important to verify that each quad is less than 256, you'll obviously require more code:

#!/bin/sh  ip=${1:-1.2.3.4}  if expr "$ip" : '[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*$' >/dev/null; then   for i in 1 2 3 4; do     if [ $(echo "$ip" | cut -d. -f$i) -gt 255 ]; then       echo "fail ($ip)"       exit 1     fi   done   echo "success ($ip)"   exit 0 else   echo "fail ($ip)"   exit 1 fi 

Or perhaps even with fewer pipes:

#!/bin/sh  ip=${1:-1.2.3.4}  if expr "$ip" : '[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*$' >/dev/null; then   IFS=.   set $ip   for quad in 1 2 3 4; do     if eval [ \$$quad -gt 255 ]; then       echo "fail ($ip)"       exit 1     fi   done   echo "success ($ip)"   exit 0 else   echo "fail ($ip)"   exit 1 fi 

Or again, if your shell is bash, you could use a cumbersome regular expression for quad validation if you're not fond of arithmetic:

#!/usr/bin/env bash  ip=${1:-1.2.3.4}  re='^(0*(1?[0-9]{1,2}|2([0-4][0-9]|5[0-5]))\.){3}'  re+='0*(1?[0-9]{1,2}|2([‌​0-4][0-9]|5[0-5]))$'  if [[ $ip =~ $re ]]; then   echo "success" else   echo "fail" fi 

This could also be expressed in BRE, but that's more typing than I have in my fingers.

And lastly, if you like the idea of putting this functionality ... in a function:

#!/usr/bin/env bash  ip=${1:-1.2.3.4}  ipvalid() {   # Set up local variables   local ip=${1:-1.2.3.4}   local IFS=.; local -a a=($ip)   # Start with a regex format test   [[ $ip =~ ^[0-9]+(\.[0-9]+){3}$ ]] || return 1   # Test values of quads   local quad   for quad in {0..3}; do     [[ "${a[$quad]}" -gt 255 ]] && return 1   done   return 0 }  if ipvalid "$ip"; then   echo "success ($ip)"   exit 0 else   echo "fail ($ip)"   exit 1 fi 

There are many ways you could do this. I've shown you just a few.

like image 198
ghoti Avatar answered Oct 09 '22 23:10

ghoti