Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 3 Aug 2013 14:04:43 +0000
From:      "Teske, Devin" <Devin.Teske@fisglobal.com>
To:        Robert Huff <roberthuff@rcn.com>
Cc:        Devin Teske <dteske@freebsd.org>, Fbsd8 <fbsd8@a1poweruser.com>, questions <questions@FreeBSD.org>
Subject:   Re: .sh script code to determine IPv4 or IPv6
Message-ID:  <13CA24D6AB415D428143D44749F57D720200229A@ltcfiswmsgmb21>
In-Reply-To: <20988.61897.78070.312049@jerusalem.litteratus.org>
References:  <51FCE9C7.7020407@a1poweruser.com> <20988.61897.78070.312049@jerusalem.litteratus.org>

Next in thread | Previous in thread | Raw E-Mail | Index | Archive | Help

On Aug 3, 2013, at 5:04 AM, Robert Huff wrote:

>=20
> Fbsd8 writes:
>=20
>> I have a .sh script that I need to determine if the entered IP
>> address is IPv4 or IPv6.
>>=20
>> Is there some .sh command that does this?
>=20
> 	Not that I know of.
> 	But ... how hard can it be to figure out whether it uses '.' or
> ':'?
>=20

Actually, there's /usr/share/bsdconfig/media/tcpip.subr

Function family:

f_validate_ipaddr6 $ipv6_addr
	# Should be complete; I digested multiple RFCs on IPv6

f_validate_ipaddr $ipv4_addr [$netmask]
	# optional netmask to validate IP is within doubly-valid
=09
f_validate_hostname $hostname
	# To RFC specifications 952 and 1123

But if you need to prompt the user to enter a value and then validate it, t=
he above functions return meaningful exit status for determining what's wro=
ng with their entry (why did it fail specification, for example).

To help decode the exit status, the functions you want to use are:

# In /usr/share/bsdconfig/networking/ipaddr.subr

Function family:

f_dialog_iperror $status $ipv4_addr
f_dialog_ip6error $status $ipv6_addr

As is implied with the "_dialog_" in their name, they take the $? exit stat=
us from the previously mentioned f_validate_*() functions and display a dia=
log(1) error appropriate to what's wrong.

For example, you might see:

ERROR! One or more individual octets within the IPv4 address\n(separated by=
 dots) contains one or more invalid characters.\nOctets must contain only t=
he characters 0-9.\n\nInvalid IP Address: %s

or

ERROR! The IP address entered has either too few (less than 3), too\nmany (=
more than 8), or not enough segments, separated by colons.\n\nInvalid IPv6 =
Address: %s

And then, in the same function family above (as the *ip[6]error()):

f_dialog_vaildate_ipaddr $ipv4_addr
f_dialog_validate_ipaddr6 $ipv6_addr

These are like:

f_validate_ipaddr $ipv4_addr
f_validate_ipaddr6 $ipv6_addr

Except as implied by the extra "_dialog_" in their name, they will actually=
 run f_validate_* and then f_dialog_ip[6]error() for you with the result.

Finally, last, but not least...

The process of actually *getting* the values has been simplified too. In th=
e same family function (as f_dialog_ip[6]error and f_dialog_validate_ipaddr=
[6]()) is:

f_dialog_input_ipaddr $interface $ipaddr
	# $interface is displayed in the prompt text
	# $ipaddr is used as default text in the input box

If user doesn't press escape or select cancel, $ipaddr will hold the users =
entry.

This function validates, displays errors, and is an all-around solution if =
you need to prompt the user to enter the info and only proceed if they ente=
r a valid entry (the above function is IPv4 centric and supports CIDR notat=
ion).

The IPv6 version of the latter (f_dialog_input_ipaddr6) does not yet exist.=
 I'm getting there. For now, if you need to prompt for an entry that could =
be IPv6, use the generic f_dialog_input() routine and sanitize it with the =
aforementioned API.
--=20
Cheers,
Devin

_____________
The information contained in this message is proprietary and/or confidentia=
l. If you are not the intended recipient, please: (i) delete the message an=
d all copies; (ii) do not disclose, distribute or use the message in any ma=
nner; and (iii) notify the sender immediately. In addition, please be aware=
 that any message addressed to our domain is subject to archiving and revie=
w by persons other than the intended recipient. Thank you.



Want to link to this message? Use this URL: <http://docs.FreeBSD.org/cgi/mid.cgi?13CA24D6AB415D428143D44749F57D720200229A>