LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   get mac address given a specific interface (https://www.linuxquestions.org/questions/programming-9/get-mac-address-given-a-specific-interface-452222/)

kpachopoulos 06-06-2006 05:02 PM

get mac address given a specific interface
 
Hi,
I'd like to get a mac address given a specific interface. How can i do this? A file of the system, the lines of which have the format "ethX mac" would be nice... However, i don't think it exists. Does anybody knoe how can i do this from C/glibc?

Thanks

b0nd 06-06-2006 09:49 PM

hi,
highligted one's are the MAC of particular NIC.

Quote:

root@bond:~# ifconfig
eth0 Link encap:Ethernet HWaddr 00:E0:4C:77:13:4F
inet addr:192.168.1.2 Bcast:192.168.1.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:1063 errors:0 dropped:0 overruns:0 frame:0
TX packets:1055 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:725489 (708.4 Kb) TX bytes:228044 (222.6 Kb)
Interrupt:12 Base address:0xab00

eth1 Link encap:Ethernet HWaddr 00:A1:B0:10:19:2E
inet addr:192.168.0.1 Bcast:192.168.0.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:0 (0.0 b) TX bytes:0 (0.0 b)
Interrupt:10 Base address:0xcc00
regards

taylor_venable 06-06-2006 09:50 PM

If you wanted to get the info from a command, ifconfig will do it. Or, you could read from the /proc/net/arp file. See man proc(5) for more info.

I'd love to test it to see if it works, but I have only BSD boxen and unfortunately my NetBSD machine with the Linux emulation doesn't have a net/arp file in the emulated Linux proc filesystem. :(

b0nd 06-06-2006 09:53 PM

Quote:

Originally Posted by taylor_venable
you could read from the /proc/net/arp file. See man proc(5) for more info.

I'd love to test it to see if it works, but I have only BSD boxen and unfortunately my NetBSD machine with the Linux emulation doesn't have a net/arp file in the emulated Linux proc filesystem. :(

on my machine its not showing any entries.

Quote:

root@bond:/proc/net# cat arp
IP address HW type Flags HW address Mask Device
root@bond:/proc/net#
regards

paulsm4 06-06-2006 10:53 PM

Hi -

The suggestion about parsing "ifconfig" is an excellent one. The way to do it from C/C++ would be with "popen ()" ("man 3 popen", search LQ, or Google for "popen()" samples).

The suggestion about using "/proc" is also a good one (although AFAIK it'll only work on Linux). Here's /proc output from my system (I've got two NICs; both are DHCP-assigned, only one of them is plugged in; the other has a null IP):
Quote:

less /proc/net/arp
IP address HW type Flags HW address Mask Device
iii.jjj.kkk.lll 0x1 0x2 00:50:8D:74:xx:xx * eth0
xxx.yyy.zzz.xxx 0x1 0x2 00:0D:72:66:xx:xx * eth0
(It's never a good idea to advertise an IP address or a MAC address, hence the "edits" to this sample output)

'Hope that helps .. PSM

randyding 06-06-2006 11:06 PM

Code:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <arpa/inet.h>



int main(int argc, char *argv[]) {
    struct ifreq ifr;
    int sock, j, k;
    char *p, addr[32], mask[32], mac[32];


    if (argc<2) {
        fprintf(stderr,"missing argument, example: eth0\n");
        return 1;
    }


    sock=socket(PF_INET, SOCK_STREAM, 0);
    if (-1==sock) {
        perror("socket() ");
        return 1;
    }


    strncpy(ifr.ifr_name,argv[1],sizeof(ifr.ifr_name)-1);
    ifr.ifr_name[sizeof(ifr.ifr_name)-1]='\0';


    if (-1==ioctl(sock, SIOCGIFADDR, &ifr)) {
        perror("ioctl(SIOCGIFADDR) ");
        return 1;
    }
    p=inet_ntoa(((struct sockaddr_in *)(&ifr.ifr_addr))->sin_addr);
    strncpy(addr,p,sizeof(addr)-1);
    addr[sizeof(addr)-1]='\0';


    if (-1==ioctl(sock, SIOCGIFNETMASK, &ifr)) {
        perror("ioctl(SIOCGIFNETMASK) ");
        return 1;
    }
    p=inet_ntoa(((struct sockaddr_in *)(&ifr.ifr_netmask))->sin_addr);
    strncpy(mask,p,sizeof(mask)-1);
    mask[sizeof(mask)-1]='\0';


    if (-1==ioctl(sock, SIOCGIFHWADDR, &ifr)) {
        perror("ioctl(SIOCGIFHWADDR) ");
        return 1;
    }
    for (j=0, k=0; j<6; j++) {
        k+=snprintf(mac+k, sizeof(mac)-k-1, j ? ":%02X" : "%02X",
            (int)(unsigned int)(unsigned char)ifr.ifr_hwaddr.sa_data[j]);
    }
    mac[sizeof(mac)-1]='\0';


    printf("\n");
    printf("name:    %s\n",ifr.ifr_name);
    printf("address: %s\n",addr);
    printf("netmask: %s\n",mask);
    printf("macaddr: %s\n",mac);
    printf("\n");


    close(sock);
    return 0;
}


kpachopoulos 06-07-2006 02:41 AM

Thanks everybody. I will probably use some of randyding's code.


All times are GMT -5. The time now is 12:17 PM.