From owner-freebsd-questions@FreeBSD.ORG Sat Aug 3 14:04:47 2013 Return-Path: Delivered-To: questions@FreeBSD.org Received: from mx1.freebsd.org (mx1.freebsd.org [8.8.178.115]) (using TLSv1 with cipher ADH-AES256-SHA (256/256 bits)) (No client certificate requested) by hub.freebsd.org (Postfix) with ESMTP id 79EE453C; Sat, 3 Aug 2013 14:04:47 +0000 (UTC) (envelope-from Devin.Teske@fisglobal.com) Received: from mx1.fisglobal.com (mx1.fisglobal.com [199.200.24.190]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.freebsd.org (Postfix) with ESMTPS id 3E87A23A7; Sat, 3 Aug 2013 14:04:46 +0000 (UTC) Received: from smtp.fisglobal.com ([10.132.206.15]) by ltcfislmsgpa04.fnfis.com (8.14.5/8.14.5) with ESMTP id r73E4jlF000645 (version=TLSv1/SSLv3 cipher=AES128-SHA bits=128 verify=NOT); Sat, 3 Aug 2013 09:04:45 -0500 Received: from LTCFISWMSGMB21.FNFIS.com ([169.254.1.135]) by LTCFISWMSGHT04.FNFIS.com ([10.132.206.15]) with mapi id 14.02.0309.002; Sat, 3 Aug 2013 09:04:44 -0500 From: "Teske, Devin" To: Robert Huff Subject: Re: .sh script code to determine IPv4 or IPv6 Thread-Topic: .sh script code to determine IPv4 or IPv6 Thread-Index: AQHOkDzj3H/8RokAXEKdt073gODRzpmDtomAgAAhnIA= Date: Sat, 3 Aug 2013 14:04:43 +0000 Message-ID: <13CA24D6AB415D428143D44749F57D720200229A@ltcfiswmsgmb21> References: <51FCE9C7.7020407@a1poweruser.com> <20988.61897.78070.312049@jerusalem.litteratus.org> In-Reply-To: <20988.61897.78070.312049@jerusalem.litteratus.org> Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-originating-ip: [10.132.253.126] Content-Type: text/plain; charset="us-ascii" Content-ID: <574A7B97ECC44442A193B569A24D6DC1@fisglobal.com> Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:5.10.8794, 1.0.431, 0.0.0000 definitions=2013-08-03_04:2013-08-02,2013-08-03,1970-01-01 signatures=0 Cc: Devin Teske , Fbsd8 , questions X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.14 Precedence: list Reply-To: Devin Teske List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 03 Aug 2013 14:04:47 -0000 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.