🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: pl.comp.os.linux.programowanie
Article #60326

Re: mac adres

#60326
From: Robert Jaroszuk
Date: Tue, 08 Nov 2005 01:00
100 lines
3393 bytes
On Mon, 07 Nov 2005, Kuba_O wrote:

> 03.11.2005, Abanowicz Tomasz:
> >  Witajcie
> >  W jaki sposob w C
> >  moge wyciagnac
> >  adresy macowe
> >  wszystkich kart
> >  sieciowych ?
> 
> Pierwsze co mi przychodzi do głowy to wywołanie ifconfig i obróbka
> wyników, nie jest to oczywiście sexowny sposób i wymaga roota. Ale co
> tam...
 
Auć... bez przesady, co? :-)

-- mac.c --

#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <errno.h>

#define VERSION "0.01"

int sock = 0;
struct ifreq iface;
u_char mac[6] = "\0\0\0\0\0\0";

void makesock(char *eth);
void showmac(char *eth);

int main(int argc, char *argv[]) {
  int option;
  char *eth = "eth0";
  extern char *optarg;

  printf ("mac by Robert Jaroszuk <zim@iq.pl>\n", VERSION);

  while ((option = getopt(argc, argv, "i:")) != EOF) {
    switch(option) {
        case 'i' :
            eth = optarg;
            break;
        default:
            exit(1);
    }
  }
  makesock(eth);
  showmac(eth);
  if(sock > 0) close(sock);
  return (0);
}

void makesock(char *eth) {
  if((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
    perror("socket");
    exit(1);
  }
  strcpy(iface.ifr_name, eth);
  if (ioctl(sock, SIOCGIFHWADDR, &iface) < 0) {
     perror(iface.ifr_name);
     exit(1);
  }
}

void showmac(char *eth) {
  int i;
  printf("Current %s MAC is: ", eth);
  for (i = 0; i < 6; i++) {
    printf("%2.2X", iface.ifr_hwaddr.sa_data[i] & 0xFF);
    if (i<5) printf(":");
  }
  printf("\n");
}

-- mac.c --

To chyba wystarczy

[7](zim@io tmp)$ ./mac -i eth0
mac by Robert Jaroszuk <zim@iq.pl>
Current eth0 MAC is: 00:0D:56:BB:5A:87
[7](zim@io tmp)$ ./mac -i eth1
mac by Robert Jaroszuk <zim@iq.pl>
Current eth1 MAC is: 00:0D:56:BB:5A:88
[7](zim@io tmp)$

-- 
... Robert Jaroszuk ...
GCS/IT/O d? s: a-- C++ ULB++++$ P+ L++++$ E--- W- K- N+ DI+ V-
w M- PS+ PE Y(+) PGP-(+++) t-- 5? X- R tv-- b++>++++ D- y+ G++
.. http://zim.iq.pl/ . RJ735-RIPE . http://zim.iq.pl/photo/ ..
... The superior warrior wins without fighting -- Sun Tzu. ...

-- 
Archiwum grupy: http://niusy.onet.pl/pl.comp.os.linux.programowanie
Technical Details
Message-ID: <20051108010019.GB26229@iq.pl>
Path: mbox-mo1.pugleaf.net!news-archive.icm.edu.pl!newsfeed.gazeta.pl!news.onet.pl!newsgate.onet.pl!newsgate.pl
References: <dkcpau$1lls$1@news.rtcomm.ru> <slrndmv983.4j1.this@localhost.localdomain>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!news-archive.icm.edu.pl!newsfeed.gazeta.pl!news.onet.pl!newsgate.onet.pl!newsgate.pl
From: zim@nospam.iq.pl (Robert Jaroszuk)
Newsgroups: pl.comp.os.linux.programowanie
Subject: Re: mac adres
Date: 8 Nov 2005 02:00:36 +0100
Organization: email<>news gateway
Lines: 101
Message-ID: <20051108010019.GB26229@iq.pl>
References: <dkcpau$1lls$1@news.rtcomm.ru> <slrndmv983.4j1.this@localhost.localdomain>
NNTP-Posting-Host: newsgate.onet.pl
Mime-Version: 1.0
X-Trace: newsgate.test.onet.pl 1131411636 13674 213.180.130.18 (8 Nov 2005 01:00:36 GMT)
X-Complaints-To: abuse@onet.pl
NNTP-Posting-Date: 8 Nov 2005 01:00:36 GMT
X-Received: (root@newsgate.test.onet.pl) by newsgate.test.onet.pl id <S115182AbVKHBA0>; Tue, 8 Nov 2005 02:00:26 +0100
X-Received: from smtp-out-io.iq.pl ([213.76.165.134]:11501 "EHLO io.iq.pl") by newsgate.test.onet.pl with ESMTP id <S115502AbVKHBAN> convert rfc822-to-8bit; Tue, 8 Nov 2005 02:00:13 +0100
X-Received: by io.iq.pl (Postfix, from userid 1000) id 3E43014A0; Tue,  8 Nov 2005 02:00:19 +0100 (CET)
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit