Re: Confusing ifreq behavior: guru needed

Riley Williams (rhw@bigfoot.com)
Wed, 14 Oct 1998 15:18:56 +0100 (GMT)


Hi James.

> I'll be concise: I am writing some user-space code that needs to
> obtain the IPv4 protocol address and hardware address from an
> interface (arbitrary). I am using the ioctl() method with
> SIOCGIFHWADDR and SIOCGIFADDR, operating on struct ifreq (contains
> struct sockaddr).

I can't help with your problem as listed, but have to admit that I
tend to spawn a shellscript for things like that. For reference,
here's the script I use that gets those details in a standard format
that I can parse, for as many interfaces as I care to list:

===8<=== CUT ===>8===
#!/bin/bash
if [ $# -lt 1 ]; then
echo "Usage: `basename $0` iface..."
exit 1
fi
function GetConfig() {
/sbin/ifconfig eth0 | sed 's/ /#/g' | tr '#' '\n' | grep ':'
}
function GetInfo() {
GetConfig "$1" | fgrep "$2" | cut -d ':' -f 2-
}
for IFACE do
if /sbin/ifconfig $IFACE > /dev/null 2>&1 ; then
HWADDR="`GetInfo $IFACE HWaddr`"
INET="`GetInfo $IFACE 'inet addr'`"
MASK="`GetInfo $IFACE 'Mask'`"
printf '%-8s <%s=%s/%s>\n' "$IFACE" "$HWADDR" "$INET" "$MASK"
else
echo "`basename $0`: ERROR: Interface $IFACE not found." >&2
fi
done
exit 0
===8<=== CUT ===>8===

In case it's relevant, my system runs RedHat 5.1 with kernel 2.0.35
and the above script also works on a system running Debian with the
same kernel...

Before anybody complains, I quote where I feel it unambiguates the
interpretation by the shell, and make NO apologies for doing so...

Best wishes from Riley.

-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.rutgers.edu
Please read the FAQ at http://www.tux.org/lkml/