LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 11-28-2010, 03:32 PM   #1
robertjinx
Member
 
Registered: Oct 2007
Location: Prague, CZ
Distribution: RedHat / CentOS / Ubuntu / SUSE / Debian
Posts: 749

Rep: Reputation: 73
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!
 
Old 11-28-2010, 03:52 PM   #2
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,466

Rep: Reputation: 451Reputation: 451Reputation: 451Reputation: 451Reputation: 451
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.
 
Old 11-28-2010, 04:27 PM   #3
robertjinx
Member
 
Registered: Oct 2007
Location: Prague, CZ
Distribution: RedHat / CentOS / Ubuntu / SUSE / Debian
Posts: 749

Original Poster
Rep: Reputation: 73
What about if the ip is 10.10.234.10?
 
Old 11-29-2010, 04:05 AM   #4
Noway2
Senior Member
 
Registered: Jul 2007
Distribution: Gentoo
Posts: 2,125

Rep: Reputation: 781Reputation: 781Reputation: 781Reputation: 781Reputation: 781Reputation: 781Reputation: 781
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.
 
Old 11-29-2010, 05:09 AM   #5
robertjinx
Member
 
Registered: Oct 2007
Location: Prague, CZ
Distribution: RedHat / CentOS / Ubuntu / SUSE / Debian
Posts: 749

Original Poster
Rep: Reputation: 73
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
}
 
Old 11-29-2010, 07:06 AM   #6
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
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
}
 
Old 11-30-2010, 01:44 AM   #7
robertjinx
Member
 
Registered: Oct 2007
Location: Prague, CZ
Distribution: RedHat / CentOS / Ubuntu / SUSE / Debian
Posts: 749

Original Poster
Rep: Reputation: 73
Hi, thx for the netmask function, it really helps.

Thanks for all the help and ideas
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Root user check in bash script doesn't work tawalker Programming 6 12-18-2011 03:06 AM
Python socket question dealing with the ip address when its user entered xskycamefalling Programming 1 03-24-2010 01:48 AM
Small bash script to notify server user makowka Linux - Server 2 02-15-2010 02:17 AM
super user privileges check for a normal user in bash script freeindy Programming 2 08-01-2008 06:08 AM
Shell Script to check root user? kushalkoolwal Programming 4 09-22-2005 12:15 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 10:28 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration