Date: Fri, 11 Feb 2011 10:08:07 -0800 From: Devin Teske <dteske@vicor.com> To: Ian Smith <smithi@nimnet.asn.au> Cc: freebsd-hackers@freebsd.org Subject: Re: [RELEASE] host-setup(1): a dialog(1)-based utility for configuring FreeBSD Message-ID: <1297447687.9144.6.camel@dt.vicor.com> In-Reply-To: <20110212015155.I96449@sola.nimnet.asn.au> References: <20110211120031.9D37510656E5@hub.freebsd.org> <20110212015155.I96449@sola.nimnet.asn.au>
next in thread | previous in thread | raw e-mail | index | archive | help
On Sat, 2011-02-12 at 02:30 +1100, Ian Smith wrote: > In freebsd-questions Digest, Vol 349, Issue 8, Message: 15 > On Thu, 10 Feb 2011 19:53:53 -0800 Devin Teske <dteske@vicor.com> wrote: > > Hi All, > > > > I'd like to announce the release of a new script. A script that I've > > developed for our field engineers that I'd like to share with the rest > > of the world. > > > > http://druidbsd.sourceforge.net/download/host-setup.txt > > > > host-setup(1) is a dialog(1)-based utility (written in sh(1)) designed > > to make configuring FreeBSD more efficient. > > Nice, if only as great bedtime reading so far; I've already learned some > new techniques. I'm particularly proud of this little diddy (see lines 1600-1607 of host-setup; rewritten to be a functional example program that takes a number like "26" and produces "255.255.255.192"): ============================================================ #!/bin/sh blen2netmask() { local nbits="$1" netmask="" n=0 while [ $n -lt 4 ]; do netmask="$netmask${netmask:+.}$(( (65280 >> ($nbits - 8 * $n) & 255) * ((8*$n) < $nbits & $nbits <= (8*($n+1))) + 255 * ($nbits > (8*($n+1))) ))" n=$(( $n + 1 )) done echo "$netmask" } blen2netmask "$@" ============================================================ I wrote _several_ other implementations and above is the one that I finally settled on as being the most elegant. I was after a solution that (a) didn't use anything but built-in internals of sh(1), and (b) got the job done as fast as computationally possible. $ time blen2netmask 26 255.255.255.192 real 0m0.004s user 0m0.001s sys 0m0.004s That's pretty fast, I'd say ^_^ (faster than the other implementations -- especially considering that it doesn't have to fork anything). -- Devin P.S. Maybe I ought to expand it to IPv6 considering that the IPv4 address space has [reportedly] finally ran out (is that true?). > I expect to steal lots of it wholesale (acknowledged :) > > cheers, Ian > _______________________________________________ > freebsd-questions@freebsd.org mailing list > http://lists.freebsd.org/mailman/listinfo/freebsd-questions > To unsubscribe, send any mail to "freebsd-questions-unsubscribe@freebsd.org"
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?1297447687.9144.6.camel>