Determining ethernet address

William Kucharski kucharsk at solbourne.com
Fri May 10 03:20:08 AEST 1991


In article <1991May9.040213.9698 at cunixf.cc.columbia.edu> micky at cunixf.cc.columbia.edu writes:
 >
 >A while back there were some postings regarding determining one's own
 >ethernet address within a program.  Of course I didn't pay attention
 >back then and now I need to know.  I don't need system independent
 >solutions right now, Sun specific would be just fine...  Actually
 >any pointers would be welcome.  If I end up writing the code, I would
 >be happy to summarize to the newsgroups...

This should work for you; it needs to be setuid root, as /dev/nit is usually
owned by root and mode 0600.

#! /bin/sh
# This is a shell archive, meaning:
# 1.  Remove everything above the #! /bin/sh line.
# 2.  Save the resulting test in a file
# 3.  Execute the file with /bin/sh (not csh) to create the files:
#
#eaddr.c
#
# Created on Thu May  9 11:17:30 MDT 1991
#
if test -f 'eaddr.c'
then
    echo shar: will not over-write existing file "eaddr.c"
else
echo extracting "eaddr.c"
sed 's/^X//' >eaddr.c <<'SHAR_EOF'
X#include <sys/types.h>
X#include <sys/file.h>
X#include <sys/ioctl.h>
X#include <sys/socket.h>
X#include <sys/time.h>
X#include <net/if.h>
X#include <net/nit_if.h>
X#include <errno.h>
X#include <fcntl.h>
X#include <stdio.h>
X
Xmain(argc, argv)
Xint argc;
Xchar *argv[];
X{
X    extern char *strcpy();
X
X    int ctr, fd;
X    struct ifreq ifr;
X
X    if (argc != 2) {
X	printf("usage: %s interface\n", argv[0]);
X	exit(1);
X    }
X
X    if ((fd = open("/dev/nit", O_RDONLY)) < 0) {
X	perror("Couldn't open /dev/nit");
X	exit(1);
X    }
X
X    (void)strcpy(ifr.ifr_name, argv[1]);
X
X    if (ioctl(fd, NIOCBIND, (char *)&ifr) < 0) {
X	perror("Couldn't bind to interface");
X	exit(1);
X    }
X
X    (void)strcpy(ifr.ifr_name, argv[1]);
X
X    if (ioctl(fd, SIOCGIFADDR, (char *)&ifr) < 0) {
X	perror("Couldn't bind to interface");
X	exit(1);
X    }
X
X    printf("Ethernet address for interface \"%s\": ", argv[1]);
X    for(ctr = 0; ctr < 6; ctr++) {
X	printf("%.2X", (ifr.ifr_addr.sa_data[ctr] & 0xff));
X	if (ctr != 5)
X	    printf(":");
X    }
X    printf("\n");
X    fflush(stdout);
X    exit(0);
X}
SHAR_EOF
if test 1050 -ne "`wc -c < eaddr.c`"
then
    echo shar: error transmitting "eaddr.c" '(should have been 1050 characters)'
fi
fi
# end of shell archive
exit 0
-- 
| William Kucharski, Solbourne Computer, Inc.     | Opinions expressed above
| Internet:   kucharsk at Solbourne.COM	          | are MINE alone, not those
| uucp:	...!{boulder,sun,uunet}!stan!kucharsk     | of Solbourne...
| Snail Mail: 1900 Pike Road, Longmont, CO  80501 | "It's Night 9 With D2 Dave!"



More information about the Comp.unix.questions mailing list