Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 28 Mar 2001 22:04:53 -0800 (PST)
From:      "Rodney W. Grimes" <freebsd@gndrsh.dnsmgr.net>
To:        clefevre@poboxes.com (Cyrille Lefevre)
Cc:        mikel@ocsinternet.com (Mikel), johnny.dang@johnnydang.net (Johnny Dang), FREEBSD-IPFW@FreeBSD.ORG (FreeBSD IpFW)
Subject:   Re: [LONG] Re: Scripting with IPFW
Message-ID:  <200103290604.WAA96937@gndrsh.dnsmgr.net>
In-Reply-To: <n1a517rt.fsf@gits.dyndns.org> from Cyrille Lefevre at "Mar 29, 2001 07:08:54 am"

next in thread | previous in thread | raw e-mail | index | archive | help
> "Rodney W. Grimes" <freebsd@gndrsh.dnsmgr.net> 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




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200103290604.WAA96937>