[RELEASE] host-setup(1): a dialog(1)-based utility for configuring FreeBSD

Devin Teske dteske at vicor.com
Fri Feb 11 18:07:59 UTC 2011


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 at 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 at freebsd.org mailing list
> http://lists.freebsd.org/mailman/listinfo/freebsd-questions
> To unsubscribe, send any mail to "freebsd-questions-unsubscribe at freebsd.org"




More information about the freebsd-hackers mailing list