The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



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

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to Validate SanjayLinux Shell Programming and Scripting 10 09-27-2007 08:16 AM
validate each field in txt happyv Shell Programming and Scripting 9 07-27-2007 06:24 AM
validate against a file chiru_h Shell Programming and Scripting 7 08-31-2006 08:15 PM
validate the file name maykap100 Shell Programming and Scripting 2 08-30-2005 11:30 AM
validate ruffenator Shell Programming and Scripting 3 01-22-2002 10:37 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 02-22-2005
sabina sabina is offline
Registered User
  
 

Join Date: Feb 2005
Posts: 11
How to validate an IP address

hi

I need a simple script to do the following.

I need to check the user input for

1. quad dotted notation.
2. check that all of the inputs are numeric and within 1-255 range.

Could someone please help me?

I tried to use egrep to check for dotted notation.

quad=1.1.1.12
QUAD4=`echo "${quad}" | egrep '[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+'`
echo $QUAD4

this validates ok excepts when I have a test ip address as "1.2.2.1a" it fails to validate if and return me the ip adrees itself.

For the second step I have no clue . I would really appreciate if anyone could give any suggestion.


Thanks,
Sabina
  #2 (permalink)  
Old 02-22-2005
zazzybob's Avatar
zazzybob zazzybob is offline Forum Advisor  
Registered Geek
  
 

Join Date: Dec 2003
Location: Melbourne, Australia
Posts: 2,100
Give this a try
Code:
#!/bin/sh

echo "Enter IP address"
read quad

oldIFS=$IFS
IFS=.
set -- $quad

if [ "$#" -ne "4" ]; then
  echo "Must have 4 parts"
  exit 1
fi

for oct in $1 $2 $3 $4; do
  echo $oct | egrep "^[0-9]+$" >/dev/null 2>&1
  if [ "$?" -ne "0" ]; then
     echo "$oct: Not numeric"
     exit 1
  else
     if [ "$oct" -lt "0" -o "$oct" -gt "255" ]; then
        echo "$oct: Out of range"
        exit 1
     fi
  fi
done

# if we're here, we're validated
echo "$quad validates OK!"
exit 0
Cheers
ZB
  #3 (permalink)  
Old 02-23-2005
sabina sabina is offline
Registered User
  
 

Join Date: Feb 2005
Posts: 11
Hi ZB

I tried it and it works pertfectly. I really like your code. It look so neat and precise. Thanks a lot for your time. I really appreciate it.

Sabina
  #4 (permalink)  
Old 02-23-2005
sabina sabina is offline
Registered User
  
 

Join Date: Feb 2005
Posts: 11
Hi ZB

I found a case where if fails.

ip=1.2.3.4.

If we have nothing after last "." if failes to recognize it as invalid ip address.

Thanks,
Sabina
  #5 (permalink)  
Old 02-23-2005
zazzybob's Avatar
zazzybob zazzybob is offline Forum Advisor  
Registered Geek
  
 

Join Date: Dec 2003
Location: Melbourne, Australia
Posts: 2,100
No problem - this should fix it....
Code:
#!/bin/sh

echo "Enter IP address"
read quad

oldIFS=$IFS
IFS=.
set -- $quad

if [ "$#" -ne "4" ]; then
  echo "Must have 4 parts"
  exit 1
fi

for oct in $1 $2 $3 $4; do
  echo $oct | egrep "^[0-9]+$" >/dev/null 2>&1
  if [ "$?" -ne "0" ]; then
     echo "$oct: Not numeric"
     exit 1
  else
     if [ "$oct" -lt "0" -o "$oct" -gt "255" ]; then
        echo "$oct: Out of range"
        exit 1
     fi
  fi
done

# New code from here....
echo "$quad" | grep "\.$" >/dev/null 2>&1
if [ "$?" -eq "0" ]; then
  echo "Trailing period - invalid"
  exit 1
fi
# to here.....

# if we're here, we're validated
echo "$quad validates OK!"
exit 0
Cheers
ZB
  #6 (permalink)  
Old 12-02-2008
real_mc real_mc is offline
Registered User
  
 

Join Date: Apr 2008
Posts: 3
Another IP for which this script would fail: 0.1.2.3

adding this helps:

if [ "$1" -eq "0" ]
then
echo "First octet must not be zero (0)..."
exit 1
fi
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 08:12 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0