Xref: utzoo comp.unix.questions:31163 alt.sys.sun:3800 Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!wuarchive!uunet!stan!kucharsk From: kucharsk@solbourne.com (William Kucharski) Newsgroups: comp.unix.questions,alt.sys.sun Subject: Re: Determining ethernet address Message-ID: <1991May9.172008.855@solbourne.com> Date: 9 May 91 17:20:08 GMT References: <1991May9.040213.9698@cunixf.cc.columbia.edu> Distribution: na Organization: Solbourne Computer, Inc., Longmont, CO Lines: 95 In article <1991May9.040213.9698@cunixf.cc.columbia.edu> micky@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 X#include X#include X#include X#include X#include X#include X#include X#include X#include 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@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!"