From owner-freebsd-ipfw Wed Mar 28 22: 5:12 2001 Delivered-To: freebsd-ipfw@freebsd.org Received: from gndrsh.dnsmgr.net (GndRsh.dnsmgr.net [198.145.92.4]) by hub.freebsd.org (Postfix) with ESMTP id 51A9737B71A for ; Wed, 28 Mar 2001 22:05:08 -0800 (PST) (envelope-from freebsd@gndrsh.dnsmgr.net) Received: (from freebsd@localhost) by gndrsh.dnsmgr.net (8.9.3/8.9.3) id WAA96937; Wed, 28 Mar 2001 22:04:54 -0800 (PST) (envelope-from freebsd) From: "Rodney W. Grimes" Message-Id: <200103290604.WAA96937@gndrsh.dnsmgr.net> Subject: Re: [LONG] Re: Scripting with IPFW In-Reply-To: from Cyrille Lefevre at "Mar 29, 2001 07:08:54 am" To: clefevre@poboxes.com (Cyrille Lefevre) Date: Wed, 28 Mar 2001 22:04:53 -0800 (PST) Cc: mikel@ocsinternet.com (Mikel), johnny.dang@johnnydang.net (Johnny Dang), FREEBSD-IPFW@FreeBSD.ORG (FreeBSD IpFW) X-Mailer: ELM [version 2.4ME+ PL54 (25)] MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Sender: owner-freebsd-ipfw@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.ORG > "Rodney W. Grimes" writes: > > > oif=fxp0 > > iif=rl0 > > oip=`ifconfig ${oif} | awk '/inet/ {print $2; exit}'` > ^ /inet / > > I would suggest you to match on inet followed by a space or > you'll match inet6 which is probably you don't want. Good catch. Forgot about that case, but then so did the code I was trying to show how to clean up a bit. ... > > > omask=`ifconfig ${oif} | awk '/inet/ {print $4; exit}'` > > iip=`ifconfig ${iif} | awk '/inet/ {print $2; exit}'` > > imask=`ifconfig ${iif} | awk '/inet/ {print $4; exit}'` > > also, these 4 lines may be rewritten as : Lets see, 4 very clean lines easy to read and understand replaced by 15 lines (3 near null so lets call it 12) of harder to read and even harder yet to figure out what it is doing. Nope, this is *sic*, 10 times more sick than grep | head | awk!!! Now granted, you did only invoke ifconfig, and awk 1 time, which would be wonderful if the old code was doing this 100's of times, but that is not the case here. This code does not need that kind of optimization, but this is a good sample of how to write a simple finite state parser in awk :-). [I would have called status, state :-)] > eval `ifconfig -a | awk -v oif="${oif}:" -v iif="${iif}:" ' > $1 == oif { status = 1 ; next } # I/O interfaces > $1 == iif { status = 3 ; next } # use odd numbers here > $1 ~ /:/ { status = 0; next } # other interfaces > !status { next } # are skipped > /inet / { status++ } # match on first IPv4 address > status == 2 { # and use even numbers there > printf "omask=%s oip=%s\n", $4, $2 > status = 0; next # skip IPv4 aliases > } > status == 4 { > printf "imask=%s iip=%s\n", $4, $2 > status = 0; next > } > '` -- Rod Grimes - KD7CAX @ CN85sl - (RWG25) rgrimes@gndrsh.dnsmgr.net To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-ipfw" in the body of the message