ip address validate routine?

ip address validate routine?

Post by halli » Mon, 14 Apr 2003 01:08:51



hello -

Does anyone have a IP validate routine after someone enters an IP address?

Appreciate it if someone would shoot it out!
thanks.hallian

 
 
 

ip address validate routine?

Post by Stephane CHAZELA » Mon, 14 Apr 2003 01:32:50



> Does anyone have a IP validate routine after someone enters an IP address?

With a POSIX sh (uses dc):

is_ip_part() {
  case $1 in
    0[xX]*) case ${1#"${1%%[!0xX]*}"} in
           ""|*[!0-9a-fA-F]*|$4) false;;
         esac;;
    0*) case ${1#"${1%%[!0]*}"} in
           *[!0-7]*|$2|$3) false;;
        esac;;
    ""|*[!0-9]*) false;;
    *) `echo "[true] [[false]]sx $1 $5 <x p" | dc`
  esac

Quote:}

is_ip() {
  (
    IFS=.
    set -- $1
    case $# in
      1) is_ip_part "$1" "????????????*" "[4-7]???????????" \
                         "?????????*" \
                         4294967295;;
      2) is_ip_part "$1" "????*" "[4-7]??" "???*" 255 &&
           is_ip_part "$2" "?????????*" z "???????*" 16777215;;
      3) is_ip_part "$1" "????*" "[4-7]??" "???*" 255 &&
           is_ip_part "$2" "????*" "[4-7]??" "???*" 255 &&
           is_ip_part "$3" "???????*" "[2-7]?????" "?????*" 65535;;
      4) is_ip_part "$1" "????*" "[4-7]??" "???*" 255 &&
           is_ip_part "$2" "????*" "[4-7]??" "???*" 255 &&
           is_ip_part "$3" "????*" "[4-7]??" "???*" 255 &&
           is_ip_part "$4" "????*" "[4-7]??" "???*" 255;;
      *) false;;
    esac
  )

Quote:}

Example usage:

if is_ip "127.0x1"; then
  echo 127.0x1 is a valid IPv4 address
else
  echo 127.0x1 is not a valid IPv4 address
fi

Note that 127.0x1 is a valid IP address, it can be canonified to
127.0.0.1. See
http://www.opengroup.org/onlinepubs/007904975/functions/inet_addr.html
for details.

--
Stphane

 
 
 

ip address validate routine?

Post by William Par » Mon, 14 Apr 2003 01:55:58



> hello -

> Does anyone have a IP validate routine after someone enters an IP address?

> Appreciate it if someone would shoot it out!
> thanks.hallian

Write your own.  4 numbers are separated by '.' (dot), so it's not that
complicated.

--

Linux solution for data management and processing.

 
 
 

ip address validate routine?

Post by Peter J. Ackl » Mon, 14 Apr 2003 02:17:08




> > Does anyone have a IP validate routine after someone enters an
> > IP address?

> Write your own.  4 numbers are separated by '.' (dot), so it's
> not that complicated.

So the IP address "3639551843" is invalid?  :-)

Try http://3639551843 and see what you get.

Peter

--
I wish dialog boxes had a butten saying "Whatever".  I hate being
forced to answer "Yes" or "No" to a question I have no opinion on
whatsoever.  There ought to be a button matching my indifference.

 
 
 

ip address validate routine?

Post by Reptilian Atroph » Tue, 13 May 2003 10:09:33





:

: > >
: > > Does anyone have a IP validate routine after someone enters an
: > > IP address?
: >
: > Write your own.  4 numbers are separated by '.' (dot), so it's
: > not that complicated.
:
: So the IP address "3639551843" is invalid?  :-)
I'd imagine so, wouldn't you ?
:
: Try http://3639551843 and see what you get.
:
: Peter
:
: --
: I wish dialog boxes had a butten saying "Whatever".  I hate being
: forced to answer "Yes" or "No" to a question I have no opinion on
: whatsoever.  There ought to be a button matching my indifference.

 
 
 

ip address validate routine?

Post by Lew Pitch » Tue, 13 May 2003 23:16:11







>:

>: > >
>: > > Does anyone have a IP validate routine after someone enters an
>: > > IP address?
>: >
>: > Write your own.  4 numbers are separated by '.' (dot), so it's
>: > not that complicated.
>:
>: So the IP address "3639551843" is invalid?  :-)
>I'd imagine so, wouldn't you ?

Of course not. IP (v4) addresses are 32bit unsigned integers, and 3639551843 is
a 32bit unsigned integer. The value given above can also be expressed in dotted
decimal form as 216.239.39.99

Lew Pitcher
IT Consultant, Enterprise Technology Solutions
Toronto Dominion Bank Financial Group

(Opinions expressed are my own, not my employers')

 
 
 

ip address validate routine?

Post by Peter J. Ackl » Wed, 14 May 2003 03:03:24




> : So the IP address "3639551843" is invalid?  :-)

> I'd imagine so, wouldn't you ?

No, I try to avoid imagining things like this.  I know, and if I
am in doubt, I read the manual page.  The IP address is valid.

If you want to be taken seriously, you ought to learn quoting
guidelines.  Your posting was such a mess that I didn't notice
your reply at once.  Check out the page

   http://www.uwasa.fi/~ts/http/quote.html

Peter

--
I wish dialog boxes had a button saying "Whatever".  I hate being
forced to answer "Yes" or "No" to a question I have no opinion on
whatsoever.  There ought to be a button matching my indifference.

 
 
 

ip address validate routine?

Post by Reptilian Atroph » Thu, 15 May 2003 09:57:16





:

: >
: > : So the IP address "3639551843" is invalid?  :-)
: >
: > I'd imagine so, wouldn't you ?
:
: No, I try to avoid imagining things like this.  I know, and if I
: am in doubt, I read the manual page.  The IP address is valid.
:
: If you want to be taken seriously, you ought to learn quoting
: guidelines.  Your posting was such a mess that I didn't notice
: your reply at once.  Check out the page
:
what ARE  you raving on about ?
:    http://www.uwasa.fi/~ts/http/quote.html
:
: Peter
:
: --
: I wish dialog boxes had a button saying "Whatever".  I hate being
: forced to answer "Yes" or "No" to a question I have no opinion on
: whatsoever.  There ought to be a button matching my indifference.

 
 
 

ip address validate routine?

Post by Reptilian Atroph » Thu, 15 May 2003 10:06:30



: On Mon, 12 May 2003 11:09:33 +1000, "Reptilian Atrophy"

:
: >



: >:

: >: > >
: >: > > Does anyone have a IP validate routine after someone enters an
: >: > > IP address?
: >: >
: >: > Write your own.  4 numbers are separated by '.' (dot), so it's
: >: > not that complicated.
: >:
: >: So the IP address "3639551843" is invalid?  :-)
: >I'd imagine so, wouldn't you ?

< snip > - - - - -

:
: Lew Pitcher
: IT Consultant, Enterprise Technology Solutions
: Toronto Dominion Bank Financial Group

Got any Hogan positions over there in the Toronto Dominion Bank, Lew ? I'd
be interested in the CAMS and Term Deposits systems if you do. I can send my
resume ? How good is the Canadian dollar at the moment ?
How much are your Hogan contractors getting p/h?

 
 
 

ip address validate routine?

Post by Peter J. Ackl » Fri, 16 May 2003 01:23:42




> :
> : If you want to be taken seriously, you ought to learn quoting
> : guidelines.  Your posting was such a mess that I didn't notice
> : your reply at once.  Check out the page
> :
> what ARE  you raving on about ?

Never mind.  You don't seem to care about the impression
you are giving anyway.

Peter

--
I wish dialog boxes had a button saying "Whatever".  I hate being
forced to answer "Yes" or "No" to a question I have no opinion on
whatsoever.  There ought to be a button matching my indifference.

 
 
 

1. Best way to validate IP address in bash?

I'm not worried about IP addresses in the form: 127.0
I can't figure out two things: 1. How to get the variable into the awk
split command; & 2. what the correct awk (I don't have gawk) regex is

code snippet
#!/usr/local/bin/bash

IPADDR=192.168.10.1
echo "IP address variable is: $IPADDR"
#
awk '{ ip=$IPADDR; split (ip,quad,".");
        for (i=1;i<5;i++)
                if (quad[i] !~ /[0-9]*/)
                printf "wrong ip address\n"
                else
                printf "correct ip address"}'

or is this a better approach?

IPADDR=192.168.10.1
echo "IP address variable is: $IPADDR"
#
if  [ awk '{/^([0-9]\.[0-9]\.[0-9]\.[0-9]/}' "${NEW_IPADDR}" ]; then
#hopefully this evaluates to true
        printf "correct ip address"
        else
        printf "wrong ip address\n"
        fi

Thanks for any help w/ this,  Tom

2. How to make mounted floppy user writeable?

3. looking for a routine to validate ipaddress

4. NIC installation pb

5. How to replace all ufs-related addresses with my own routine addresses ?

6. TNT2 video with VMware, how to load kernel module?

7. How to replace all UFS related addresses with my own routine addresses ?

8. MCA sysfs part III - moving dma_mask

9. Changing IP address on Solaris 9 with multiple virtual IP addresses

10. WANTED: pingmac <IP ADDR> which returns <MAC ADDRESS of IP ADDRESS>

11. dynamic ip address - how to get to know my ip address?

12. IP Masquerading with IP Address and Mac Address Restrictions

13. [Fwd: How to validate an address?]