Linux - Server This forum is for the discussion of Linux Software used in a server related context. |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
11-28-2010, 03:32 PM
|
#1
|
Member
Registered: Oct 2007
Location: Prague, CZ
Distribution: RedHat / CentOS / Ubuntu / SUSE / Debian
Posts: 749
Rep:
|
Small script to check if what the user entered is an ip or not
Hello, does anyone know how to do a small script that checks if what the users entered is an ip address or not? Also possible to do the same but instead of an ip address is a netmask.
For example the user entered: 192.168.0.1 so its true this is an ip address, if it doesn't look like an ip address then is false
And second example, the user entered: 255.255.255.0 so the script returns true, and if its not a netmask the script returns false.
If anyone knows how to do this, I will really appreciate the help.
Thank you!
|
|
|
11-28-2010, 03:52 PM
|
#2
|
Senior Member
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,466
|
Code:
egrep "[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}"
But this allows illegal addresses as well, as 999.999.999.999
Last edited by Guttorm; 11-28-2010 at 03:53 PM.
|
|
|
11-28-2010, 04:27 PM
|
#3
|
Member
Registered: Oct 2007
Location: Prague, CZ
Distribution: RedHat / CentOS / Ubuntu / SUSE / Debian
Posts: 749
Original Poster
Rep:
|
What about if the ip is 10.10.234.10?
|
|
|
11-29-2010, 04:05 AM
|
#4
|
Senior Member
Registered: Jul 2007
Distribution: Gentoo
Posts: 2,125
|
The technique to use, which Guttorm was pointing to, is called regular expressions. Regular expressions are a pattern matching syntax that uses symbols to represent logical pattern constructs. For example, in the sample provided in the earlier post, "[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}", the [] are called character classes where you match on the range of characters in the block. This is followed the {1,3} which means that there must be at least 1 of these and no more than 3. the \. means that this should then be followed by a literal . character, which is escaped with the \ because it is normally a wild card that matches a single character.
Putting this all together then and repeating the three times says to match a set of three, 1 to 3 digit numbers with periods in between. As was pointed out, though, this will match numbers like 999.999.999.999, which are not valid. A similar pattern could be used with a netmask.
The problem with any type of pattern matching like this is that it gets really tricky to be selective, as the 999 example shows and this results in more complex expressions, which can include things like conditionals. What you can do is use the expression to match and verify the form of the input and then use your application code to verify the range and validity of the data.
You didn't say what your application is programmed in, but many recent programming languages, such as PHP and PERL, have support for regular expressions as do utilities such as egrep, sed, awk, etc.
|
|
|
11-29-2010, 05:09 AM
|
#5
|
Member
Registered: Oct 2007
Location: Prague, CZ
Distribution: RedHat / CentOS / Ubuntu / SUSE / Debian
Posts: 749
Original Poster
Rep:
|
I found a small function which works quite well:
Code:
valid_ip()
{
local ip=$1
local stat=1
if [[ $ip =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
OIFS=$IFS
IFS='.'
ip=($ip)
IFS=$OIFS
[[ ${ip[0]} -le 255 && ${ip[1]} -le 255 \
&& ${ip[2]} -le 255 && ${ip[3]} -le 255 ]]
stat=$?
fi
return $stat
}
|
|
|
11-29-2010, 07:06 AM
|
#6
|
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
|
Nice looking function and thanks for sharing it.
Two concerns for completeness, though: - The function validates broadcast and network addresses as IP addresses. OK?
- ISTR that IP address octets, normally given in decimal, can also be given in octal in which case they must begin with 0. If true this means that a valid octet with decimal value 64 to 255 would have four octal digits.. Maybe you don't want to support IP addresses expressed in octal. If that is so, the validating regex becomes ^([1-9][0-9]{1,2})|(0)\. and so on (assuming you don't want to accept octets of 00 and 000 which you might because they are valid if perverse).
In the OP you asked for netmask validation too:
Code:
validate_netmask()
{
local netmask=$1
local netmask_binary
local octet
local stat
if [[ $netmask =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
stat=0
for ((i=0; i<4; i++))
do
octet=${netmask%%.*}
netmask=${netmask#*.}
[[ $octet -gt 255 ]] && { stat=1; break; }
netmask_binary=$netmask_binary$( echo "obase=2; $octet" | bc )
[[ $netmask_binary =~ 01 ]] && { stat=1; break; }
done
else
stat=1
fi
return $stat
}
|
|
|
11-30-2010, 01:44 AM
|
#7
|
Member
Registered: Oct 2007
Location: Prague, CZ
Distribution: RedHat / CentOS / Ubuntu / SUSE / Debian
Posts: 749
Original Poster
Rep:
|
Hi, thx for the netmask function, it really helps.
Thanks for all the help and ideas 
|
|
|
All times are GMT -5. The time now is 10:28 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|