[lkml]   [1998]   [Oct]   [14]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
Messages in this thread
SubjectRe: Confusing ifreq behavior: guru needed
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===
if [ $# -lt 1 ]; then
echo "Usage: `basename $0` iface..."
exit 1
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"
echo "`basename $0`: ERROR: Interface $IFACE not found." >&2
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
Please read the FAQ at

 \ /
  Last update: 2005-03-22 13:45    [W:0.078 / U:1.924 seconds]
©2003-2018 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site