|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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
|
||||
|
||||
|
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
|
|||
|
|||
|
Quote:
Sorry, is this supposed to return something when run, because it doesnt? |
|
#4
|
|||
|
|||
|
|
| Sponsored Links | |
|
|
#5
|
|||
|
|||
|
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
|
||||
|
||||
|
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
|
||||
|
||||
|
Quote:
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 | ||
|
![]() |
| Thread Tools | Search this Thread |
| 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 |
|
|