Validating IP address.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Validating IP address.
# 1  
Old 07-22-2008
Validating IP address.

Hi All,
This is a small snippet that I am using to validate the format of IP address.This is working fine.
Since this is not elegant, please let me know if anyone has a better working code than the one below.

Also I am planning to ping the ip address given by the user and check the $? variable. Also let me know if anyone has a better way out, to check if the ip address is on the network.

Thanks!
nua7

Code:
IP_ADDR_VAL=$(echo "$ipadd" | grep -Ec '^(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])')
if [ $IP_ADDR_VAL -eq 0 ]; then
    echo -e "*** Bad ip address: $ipadd\nTry again with correct IP Address.\nNo data has been entered into DNS."
    exit 2
fi

# 2  
Old 07-22-2008
As a stylistic issue, I would dissuade from the use of grep -c and [ xxx -eq 0 ] -- especially on $? -- in favor of something like

Code:
if ! echo "$ipadd" | grep -E '...' >/dev/null; then
  echo -e "*** Bad ..." >&2
  exit 2
fi

Also note the redirection of the error message to file descriptor 2. You might want to add $0 to the beginning of the error message as well.

The use of {1} in the regular expression is obviously redundant. In order to reduce backtracking, you might want to use [01][0-9]{0,2} to cover all strings beginning with 0 or 1 in one go, aand adjust the subsequent expression accordingly.
# 3  
Old 07-22-2008
Note that this regular expression validates the format of an IPv4 address but does not work for an IPv6 address.
# 4  
Old 07-22-2008
Thanks a lot!

Thanks era and fpmurphy for all the suggestions!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

C program to detect duplicate ip address if any after assigning ip address to ethernet interface

Hi , Could someone let me know how to detect duplicate ip address after assigning ip address to ethernet interface using c program (3 Replies)
Discussion started by: Gopi Krishna P
3 Replies

2. Shell Programming and Scripting

validating(pingable or not) remote ip address in shell script

i need to verify whether the ip adress given as input to the shell script is pingable or not... that is whether the ip is alive and responding.. ping $ip_adress the above wont work in script because the execution is continuous... so the shell script keeps will dwell in this pinging process...... (8 Replies)
Discussion started by: vivek d r
8 Replies

3. Shell Programming and Scripting

awk for validating

HI, I am trying to write a validation script as below awk '($1=="ABC"&&$2="XYZ" ,then check the value in $8,$10 these columns should not be null. so their should be some corresponding value n the $8 and $10 column,if their is no value the script has to give error saying that at a... (2 Replies)
Discussion started by: gaur.deepti
2 Replies

4. UNIX for Dummies Questions & Answers

Validating a file

Pardon my ignorance but I am lost on how to do this I have a file called "Sample.txt", it is pipe delimited, it should have 13 fields. But some of the records do not, I would like to set up a shell script where I can pass in a parameter "Sample.txt" and it would split the file into records... (2 Replies)
Discussion started by: dgeehot
2 Replies

5. UNIX for Dummies Questions & Answers

Panic kernal-mode address fault on user address 0x14

:) Firstly Hi all!!, im NEW!! and on here hoping that someone might be able to offer me some help... i have a server that keeps crashing every few days with the error message: PANIC KERNAL-MODE ADDRESS FAULT ON USER ADDRESS 0X14 KERNAL PAGE FAULT FROM (CS:EIP)=(100:EF71B5BD) EAX=EF822000... (10 Replies)
Discussion started by: Twix
10 Replies

6. Shell Programming and Scripting

validating 10. IP address any way possible

HELP: validating IP addresses any way possible -------------------------------------------------------------------------------- I am trying to validate input from the user in a script. I thought is was easy to do using regular expressions but I can't figure out how to use REs in a... (1 Reply)
Discussion started by: nrodolfich
1 Replies

7. Shell Programming and Scripting

Validating $1 and $2 before using

Hi there, I'm trying to validate my $1 and $2 before I use them in the sqlplus update, I think I have the validation rules set correctly??? however when I test my script, it jumps straight into the sql. It doesn't seem to validate the vars....even when I know they are incorrect. if ; then ... (6 Replies)
Discussion started by: nhatch
6 Replies

8. UNIX for Dummies Questions & Answers

validating userid

What's wrong with this syntax? It's part of my 'if' statement but it doesn't seem to pass and it keeps going to the 'else' part. I thought it says that userid must start with a non-numeric character and is between 6 and 10 characters long (alphanumeric). $userid|grep -Eq '^?\{6,10\}+$' if... (2 Replies)
Discussion started by: giannicello
2 Replies

9. Programming

validating input

how do i validate y script so that it only accepts values between 1 and 3 and against any character input, cause at the moment i can only validate against numbers outside 1 and 3 but not characters cheers (4 Replies)
Discussion started by: ruffenator
4 Replies

10. Shell Programming and Scripting

validating dates

This is what I have to check date entries in an interactive script with the end users... I use this to build control cards for a reporting utility supplied by a software vendor. I also want to check to make sure its a valid day based on the month (ie 30days has sept, april, june and Nov..)... ... (2 Replies)
Discussion started by: MizzGail
2 Replies
Login or Register to Ask a Question