> # get the physical device that leads to the default gateway
> netstat -nr | awk '$1 == "0.0.0.0" && $3 == "0.0.0.0" { print $NF }'
> # becomes this without netstat or awk
> ip route | sed -e '/^default/!d' -e 's/^.* ([^ ]*) $/\1/'

This seems OK, although it prints multiple lines if you've got multiple
default gateways (which happens by default on RedHat if you assign IP
aliases), of course so does the FreeS/WAN code, so that's probably OK...

> # set up some vars pertaining to $phys
> eval `ifconfig $phys | awk '$1 == "inet" && $2 ~ /^addr:/ && $4 ~ /^Mask:/
{
> gsub(/:/, " ", $0)
> print "addr=" $3
> if ($4 == "Bcast")
> print "type=broadcast"
> else if ($4 == "P-t-P")
> print "type=pointopoint"
> else
> print "type="
> print "otheraddr=" $5
> print "mask=" $7
> }'`
> # becomes (something like) this without ifconfig or awk
> eval `ip addr show dev $phys | sed -e '/^ *inet/!d' -e 's/^ *inet
\(.*\)\/\(.*\) \(.*\) \(.*\) \(.*\) \(.*\)
\(.*\)/addr=\1;mask=\2;type=\3;otheraddr=\4;/'`
> # of course, the type and mask are not the right format

This is a reasonable way to tackle this.  Breaking up the fields is
something more appropriate for the set command (with "/" added to IFS), but
you still need sed (or something) to strip all but the last line, so parsing
the variables with sed isn't really all that bad.

Also, sed likes to match as much as possible when pattern matching.  I'd
probably use something slightly less ambiguous than .* for the field
specifiers.  Since you're parsing on spaces, using [^ ] should work well,
and will guarantee sed won't get too aggressive and put a bunch of fields
into one variable if the ip addr output changes due to some unforseen
configuration...this is where using set might be safer and easier to
understand (and maintain):

IFS="$IFS/"
set -- `ip addr show dev $phys | sed '/^ *inet/!d'`
addr=$2
mask=$3
type=$4
otheraddr=$5

Any particular reason you're trying to get rid of ifconfig and awk?  Care to
share what you're working on with the class?

Charles Steinkuehler
http://lrp.steinkuehler.net
http://c0wz.steinkuehler.net (lrp.c0wz.com mirror)


_______________________________________________
Leaf-user mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/leaf-user

Reply via email to