IP address validation function

Tags
shell scripts

 
Thread Tools Search this Thread
# 1  
Old 03-13-2007
IP address validation function

Hi does anybody have a ksh/sh/bash function that i can embed into my script that i could use to validate an inputted IP address, I tried using one big long regular expression but it got very long and complicated

ie

Code:
#!/bin/ksh
echo " Please enter your IP address"
read IP
 ---some function to determine if it is valid or not

any help would be greatly appreciated
# 2  
Old 03-13-2007
Code:
echo 190.04.50.00 | awk -F"\." ' $0 ~ /^([0-9]{1,3}\.){3}[0-9]{1,3}$/ && $1 <=255 && $2 <= 255 && $3 <= 255 && $4 <= 255 '

# 3  
Old 03-13-2007
Quote:
Originally Posted by anbu23
Code:
echo 190.04.50.00 | awk -F"\." ' $0 ~ /^([0-9]{1,3}\.){3}[0-9]{1,3}$/ && $1 <=255 && $2 <= 255 && $3 <= 255 && $4 <= 255 '


Sorry, is this supposed to return something when run, because it doesnt?
# 4  
Old 03-13-2007
# 5  
Old 03-13-2007
thanks, but could you explain how I can integrate that into my script

The following returns nothing when I enter a valid IP address

Code:
#!/bin/ksh
echo input IP address
read IP
result=`echo $IP | awk -F"\." ' $0 ~ /^([0-9]{1,3}\.){3}[0-9]{1,3}$/ && $1 <=255 && $2 <= 255 && $3 <= 255 && $4 <= 255 '`
echo $result

~
Code:
bash# ./test.script
input IP address
10.10.10.10      < this is my input
                      < see nothing returned
bash#

# 6  
Old 03-13-2007
Remove colored code
Code:
#!/bin/ksh
echo input IP address
read IP
result=`echo $IP | awk -F"\." ' $0 ~ /^([0-9]{1,3}\.){3}[0-9]{1,3}$/ && $1 <=255 && $2 <= 255 && $3 <= 255 && $4 <= 255 '`
echo $result

# 7  
Old 03-13-2007
Quote:
Originally Posted by hcclnoodles
Hi does anybody have a ksh/sh/bash function that i can embed into my script that i could use to validate an inputted IP address, I tried using one big long regular expression but it got very long and complicated

ie

Code:
#!/bin/ksh
echo " Please enter your IP address"
read IP
 ---some function to determine if it is valid or not

Assuming a dotted quad format:

Code:
valid_dotted_quad()
{
    ERROR=0
    oldIFS=$IFS
    IFS=.
    set -f
    set -- $1
    if [ $# -eq 4 ]
    then
      for seg
      do
        case $seg in
            ""|*[!0-9]*) ERROR=1;break ;; ## Segment empty or non-numeric char
            *) [ $seg -gt 255 ] && ERROR=2 ;;
        esac
      done
    else
      ERROR=3 ## Not 4 segments
    fi
    IFS=$oldIFS
    set +f
    return ERROR
}

Test $IP with:

Code:
if valid_dotted_quad "$IP"
then
   ## IP OK
else
   ## Not OK
  printf "%s is not a valid dotted quad IP address" "$IP" >&2
fi

Note, however, that other formats can be valid IP addresses.

|
Thread Tools Search this Thread
Search this Thread:
Advanced Search

More UNIX and Linux Forum Topics You Might Find Helpful
Will files, creaetd in one function of the same script will be recognized in another function? digioleg54 Shell Programming and Scripting 1 03-02-2017 11:30 AM
C program to detect duplicate ip address if any after assigning ip address to ethernet interface Gopi Krishna P UNIX for Advanced & Expert Users 3 08-19-2015 12:43 PM
How to step in one function after the function be executed in gdb? 915086731 Programming 4 10-31-2011 05:22 AM
What is the function to get address of the virtual memory block in linux?? powyama UNIX for Advanced & Expert Users 1 09-28-2011 02:04 AM
Tracing a MAC address to IP address: Solaris DNT IP Networking 9 08-17-2010 09:10 AM
What would the physical address be for virtual address? lemon_06 UNIX for Dummies Questions & Answers 0 06-09-2010 09:05 AM
function trace back and address to line number conversion Wkdunreal AIX 0 10-16-2009 02:20 AM
Panic kernal-mode address fault on user address 0x14 Twix UNIX for Dummies Questions & Answers 10 06-15-2009 10:42 AM
ksh - how to list all ip address between 2 ip address frustrated1 Shell Programming and Scripting 4 06-12-2009 10:03 AM
EMail Address Validation (Shell Script) balajiora Shell Programming and Scripting 3 05-20-2009 04:41 PM
Passing global variable to a function which is called by another function sars Shell Programming and Scripting 4 06-30-2008 12:39 PM
regular expression for MAC address validation hcclnoodles Shell Programming and Scripting 21 11-08-2007 07:14 AM
How to Achive IP address through MAC(Ethernet) address krishnacins IP Networking 3 08-29-2005 09:45 PM
network address and broadcast address? pnxi UNIX for Dummies Questions & Answers 7 11-10-2003 11:29 PM