IP address validation function | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

IP address validation function

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 03-13-2007
hcclnoodles hcclnoodles is offline
Registered User
 
Join Date: Mar 2002
Last Activity: 13 June 2014, 9:36 AM EDT
Posts: 294
Thanks: 0
Thanked 1 Time in 1 Post
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
Sponsored Links
    #2  
Old 03-13-2007
anbu23's Avatar
anbu23 anbu23 is offline Forum Advisor  
Advisor
 
Join Date: Mar 2006
Last Activity: 20 May 2014, 5:16 AM EDT
Location: Bangalore,India
Posts: 2,089
Thanks: 12
Thanked 131 Times in 126 Posts

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 '

Sponsored Links
    #3  
Old 03-13-2007
hcclnoodles hcclnoodles is offline
Registered User
 
Join Date: Mar 2002
Last Activity: 13 June 2014, 9:36 AM EDT
Posts: 294
Thanks: 0
Thanked 1 Time in 1 Post
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
matrixmadhan matrixmadhan is offline Forum Advisor  
Technorati Master
 
Join Date: Mar 2005
Last Activity: 5 July 2014, 11:14 AM EDT
Location: classification algos
Posts: 3,203
Thanks: 19
Thanked 30 Times in 27 Posts
check this post
Sponsored Links
    #5  
Old 03-13-2007
hcclnoodles hcclnoodles is offline
Registered User
 
Join Date: Mar 2002
Last Activity: 13 June 2014, 9:36 AM EDT
Posts: 294
Thanks: 0
Thanked 1 Time in 1 Post
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#

Sponsored Links
    #6  
Old 03-13-2007
anbu23's Avatar
anbu23 anbu23 is offline Forum Advisor  
Advisor
 
Join Date: Mar 2006
Last Activity: 20 May 2014, 5:16 AM EDT
Location: Bangalore,India
Posts: 2,089
Thanks: 12
Thanked 131 Times in 126 Posts
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

Sponsored Links
    #7  
Old 03-13-2007
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
 
Join Date: Mar 2007
Last Activity: 25 July 2014, 8:14 PM EDT
Location: Toronto, Canada
Posts: 2,877
Thanks: 0
Thanked 110 Times in 102 Posts
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.
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
What is the function to get address of the virtual memory block in linux?? powyama UNIX for Advanced & Expert Users 1 09-28-2011 01:04 AM
function trace back and address to line number conversion Wkdunreal AIX 0 10-16-2009 01:20 AM
EMail Address Validation (Shell Script) balajiora Shell Programming and Scripting 3 05-20-2009 03:41 PM
regular expression for MAC address validation hcclnoodles Shell Programming and Scripting 21 11-08-2007 06:14 AM



All times are GMT -4. The time now is 10:58 PM.