.sh script code to determine IPv4 or IPv6
Devin.Teske at fisglobal.com
Sat Aug 3 14:04:47 UTC 2013
On Aug 3, 2013, at 5:04 AM, Robert Huff wrote:
> Fbsd8 writes:
>> I have a .sh script that I need to determine if the entered IP
>> address is IPv4 or IPv6.
>> Is there some .sh command that does this?
> Not that I know of.
> But ... how hard can it be to figure out whether it uses '.' or
Actually, there's /usr/share/bsdconfig/media/tcpip.subr
# Should be complete; I digested multiple RFCs on IPv6
f_validate_ipaddr $ipv4_addr [$netmask]
# optional netmask to validate IP is within doubly-valid
# To RFC specifications 952 and 1123
But if you need to prompt the user to enter a value and then validate it, the above functions return meaningful exit status for determining what's wrong 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
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 status from the previously mentioned f_validate_*() functions and display a dialog(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 the characters 0-9.\n\nInvalid IP Address: %s
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 *iperror()):
These are like:
Except as implied by the extra "_dialog_" in their name, they will actually run f_validate_* and then f_dialog_iperror() for you with the result.
Finally, last, but not least...
The process of actually *getting* the values has been simplified too. In the same family function (as f_dialog_iperror and f_dialog_validate_ipaddr()) 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 enter a valid entry (the above function is IPv4 centric and supports CIDR notation).
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.
The information contained in this message is proprietary and/or confidential. If you are not the intended recipient, please: (i) delete the message and all copies; (ii) do not disclose, distribute or use the message in any manner; and (iii) notify the sender immediately. In addition, please be aware that any message addressed to our domain is subject to archiving and review by persons other than the intended recipient. Thank you.
More information about the freebsd-questions