LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 06-06-2006, 05:02 PM   #1
kpachopoulos
Member
 
Registered: Feb 2004
Location: Athens, Greece
Distribution: Gentoo,FreeBSD, Debian
Posts: 704

Rep: Reputation: 30
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
 
Old 06-06-2006, 09:49 PM   #2
b0nd
Senior Member
 
Registered: Jan 2005
Distribution: Slackware, BackTrack, Windows XP
Posts: 1,020

Rep: Reputation: 45
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
 
Old 06-06-2006, 09:50 PM   #3
taylor_venable
Member
 
Registered: Jun 2005
Location: Indiana, USA
Distribution: OpenBSD, Ubuntu
Posts: 892

Rep: Reputation: 40
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.
 
Old 06-06-2006, 09:53 PM   #4
b0nd
Senior Member
 
Registered: Jan 2005
Distribution: Slackware, BackTrack, Windows XP
Posts: 1,020

Rep: Reputation: 45
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
 
Old 06-06-2006, 10:53 PM   #5
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
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

Last edited by paulsm4; 06-06-2006 at 10:55 PM.
 
Old 06-06-2006, 11:06 PM   #6
randyding
Member
 
Registered: May 2004
Posts: 552

Rep: Reputation: 31
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;
}
 
Old 06-07-2006, 02:41 AM   #7
kpachopoulos
Member
 
Registered: Feb 2004
Location: Athens, Greece
Distribution: Gentoo,FreeBSD, Debian
Posts: 704

Original Poster
Rep: Reputation: 30
Thanks everybody. I will probably use some of randyding's code.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to find an IP address from the MAC address of a remote machine ? jitz Linux - General 3 01-03-2006 07:55 AM
How to find IP address of a machine if I know their MAC Address dysenteryduke Linux - Networking 13 09-12-2005 10:21 AM
MAC Address spoofing on alias/secondary interface tara Linux - Networking 3 08-31-2005 09:22 PM
how to get ip address, broadcast address, mac address of a machine sumeshstar Programming 2 03-12-2005 04:33 AM
DHCP Server MAC Address found, IP address not assigned wmburke Linux - Wireless Networking 17 11-17-2004 10:33 AM


All times are GMT -5. The time now is 05:04 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration