TOP
SSV SOFTWARE SYSTEMS REGISTER  REGISTER
Log in to check your private messages  Log in to check your private messages
START FAQ SEARCH MITGLIEDER PROFILE  Log in 
SSV-Forum
MAC-Adresse auslesen …

 
Post new topic   Reply to topic    SSV-Forum Forum Index >>> DNP/5282
<<< Previous topic - Next topic >>>  
Display posts from previous:   
Author Message
kdw



Joined: 05 May 2006
Posts: 1490

PostPosted: 25.03.2013, 12:08    Post subject: MAC-Adresse auslesen … Reply with quote

Hallo Forum.

Hin und wieder kommt die Frage auf, wie man mit einer in C geschriebenen Anwendung die MAC-Adresse der eth0/LAN1-Schnittstelle auslesen kann. Hier ein Beispiel:

Code:
#include <unistd.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <sys/ioctl.h>
#include <net/if.h>
 
int main(void) {
   int sock;             //socket descriptor
   struct ifreq ifr;     //network interface structure
   uint8_t *hw = NULL;   //pointer to hardware address

   //open eth0 interface and read out the MAC address
   
   if ((sock = socket(PF_INET, SOCK_STREAM, 0)) != -1) {
       strcpy(ifr.ifr_name, "eth0");
       if (ioctl(sock, SIOCGIFHWADDR, &ifr) != -1) {
           hw = (uint8_t *)(ifr.ifr_hwaddr.sa_data);
           printf("mac eth0: %02X:%02X:%02X:%02X:%02X:%02X\n",
                  *(hw), *(hw + 1), *(hw + 2), *(hw + 3),
                  *(hw + 4), *(hw + 5));
       }
       close(sock);
   }
}

Gruß

KDW
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    SSV-Forum Forum Index >>> DNP/5282 All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

SSV SOFTWARE SYSTEMS

Dünenweg 5
30419 Hannover

Fon: +49(0)511  ·  40 000-0
Fax: +49(0)511  ·  40 000-40

sales@ssv-embedded.de


Impressum    ·    Datenschutz    ·    AGB

© 2024 SSV SOFTWARE SYSTEMS GmbH. Alle Rechte vorbehalten.

ISO 9001:2015