Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Q: How to get ethernet address(es) in Linux ?

0 views
Skip to first unread message

Joe Leherbauer

unread,
Sep 15, 1995, 3:00:00 AM9/15/95
to
Is there a way to get the ethernet address(es) from a C program
or from the shell ?

And is it possible to check whether there is an ethernet card installed
at all ?

--
Josef Leherbauer | And now for something completely different ...
j...@TakeFive.co.at | a man with a tape recorder in his nose.

Thomas Gschwind

unread,
Sep 15, 1995, 3:00:00 AM9/15/95
to
Joe Leherbauer (j...@TakeFive.co.at) wrote:
: Is there a way to get the ethernet address(es) from a C program

: or from the shell ?

: And is it possible to check whether there is an ethernet card installed
: at all ?

/sbin/ifconfig eth0 | awk '/HWaddr/ { print $6 }'

Thomas

--
Thomas GSCHWIND, Student at Technische Universität WIEN
--oOo--(^)--oOo-- email: t...@logic.tuwien.ac.at
(o o) URL: http://fbma.tuwien.ac.at/~e9225658/
/|\ DOS is too DOSASTROUS for you? Try UN*X!

Herbert Rosmanith

unread,
Sep 17, 1995, 3:00:00 AM9/17/95
to
Joe Leherbauer (j...@TakeFive.co.at) wrote:
: Is there a way to get the ethernet address(es) from a C program
: or from the shell ?

I call this one "ifhwaddr":

----------------------- >8 ---------------------------------------------

/*
* compile: cc -O3 -N -s -o ifhwaddr ifhwaddr.c
*
* print hardware address of a given device and exit
*
* (c) he...@wildsau.idv.uni-linz.ac.at
*/

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <net/if.h>
#include <errno.h>

void main(int argc,char *argv[]) {
struct ifreq ifr;
u_char *b;
int s;

if (argc!=2) {
fprintf(stderr,"usage: %s <interface>\n",argv[0]);
exit(-1);
}

s=socket(AF_INET,SOCK_DGRAM,0);
if (s==-1) {
perror("socket");
exit(-1);
}

strcpy((char *)&ifr.ifr_ifrn,argv[1]);
if (ioctl(s,SIOCGIFHWADDR,&ifr)==-1) {
perror("ioctl");
exit(-1);
}
close(s);

b=(u_char *)&ifr.ifr_ifru.ifru_hwaddr.sa_data;
printf("%.2x",*b++);
for (s=1;s<IFHWADDRLEN;s++) printf(":%.2x",*b++);
printf("\n");
}

: And is it possible to check whether there is an ethernet card installed
: at all ?

just type "ifconfig eth0" , you'll see either the net-device configuration
or a message saying "unknown interface".

--
-------------------------------------------------------------
he...@wildsau.idv.uni-linz.ac.at | Fighting for peace is like
Rosm...@Edvz.uni-linz.ac.at | fucking for virginity

0 new messages