如何在Linux下取得MAC地址

梦断酒醒 2010-06-17 06:33:34
如何在Linux下取得MAC地址?在网上找了一些代码,但是这些代码有一个共同点,那就是如果网卡的IP地址没有指定的话,则不能取得该网卡的MAC地址,有没有办法在不为网卡指定IP地址的情况下直接取得MAC地址呢?
...全文
516 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
doukou87 2010-08-19
  • 打赏
  • 举报
回复
ifconfig eth0/网卡名称
梦断酒醒 2010-06-18
  • 打赏
  • 举报
回复
继续求助
lzh3ng 2010-06-18
  • 打赏
  • 举报
回复
关注中
别逗我乐 2010-06-18
  • 打赏
  • 举报
回复
帮顶
npngsc 2010-06-18
  • 打赏
  • 举报
回复
三楼正解,是用ioctl()来获得的
Proteas 2010-06-18
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 do_fork 的回复:]

遇到这个问题,应该想想有什么样的软件带这个功能,立刻能想到ifconfig,
然后找一下ifconfig在哪个包里,就会找到net-tools,然后把net-tools包的源码下载下来,
Debian系的Linux下载源码很方便(apt-get source net-tools)即可,
然后打开ifconfig.c开始分析源码。

如果分析源码觉得太麻烦,
可以strace ifco……
[/Quote]
顶!
梦断酒醒 2010-06-18
  • 打赏
  • 举报
回复
在未为网卡设定的IP的情况下,ifconfig也不能显示出这些网卡的信息。

[Quote=引用 6 楼 do_fork 的回复:]

遇到这个问题,应该想想有什么样的软件带这个功能,立刻能想到ifconfig,
然后找一下ifconfig在哪个包里,就会找到net-tools,然后把net-tools包的源码下载下来,
Debian系的Linux下载源码很方便(apt-get source net-tools)即可,
然后打开ifconfig.c开始分析源码。

如果分析源码觉得太麻烦,
可以strace ifco……
[/Quote]
梦断酒醒 2010-06-18
  • 打赏
  • 举报
回复
谢谢你的回复,我现在就用的类似这样的代码,但是问题在于eth0这样的名称是固定的么?我的应用是可能存在有线或无线网卡或两者都有的情况。希望能进一步说明,谢谢。

[Quote=引用 3 楼 xiaowai0219 的回复:]

C/C++ code
#include <stdio.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include<unistd.h>

int main(int argc, char *argv[])
{
struct ifreq ifreq;
int sockfd;
un……
[/Quote]
赵4老师 2010-06-18
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 do_fork 的回复:]
遇到这个问题,应该想想有什么样的软件带这个功能,立刻能想到ifconfig,
然后找一下ifconfig在哪个包里,就会找到net-tools,然后把net-tools包的源码下载下来,
Debian系的Linux下载源码很方便(apt-get source net-tools)即可,
然后打开ifconfig.c开始分析源码。

如果分析源码觉得太麻烦,
可以strace ifcon……
[/Quote]
弓虽 丁页!
中东和平路线图咋没请阁下设计捏?(^_^)
do_fork 2010-06-18
  • 打赏
  • 举报
回复
还有一个60%可行,但不值得使用的方法
$ cat /etc/udev/rules.d/70-persistent-net.rules 
# This file maintains persistent names for network interfaces.
# See udev(7) for syntax.
#
# Entries are automatically added by the 75-persistent-net-generator.rules
# file; however you are also free to add your own entries.

# PCI device 0x10ec:0x8168 (r8169)
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="00:26:18:a2:16:eb", ATTR{dev_id}=="0x0", ATTR{type}=="1", KERNEL=="eth*", NAME="eth0"

# PCI device 0x168c:0x001c (ath5k)
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="00:24:2c:ad:70:ae", ATTR{dev_id}=="0x0", ATTR{type}=="1", KERNEL=="wlan*", NAME="wlan0"

do_fork 2010-06-18
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 ishallwin 的回复:]

在未为网卡设定的IP的情况下,ifconfig也不能显示出这些网卡的信息。

引用 6 楼 do_fork 的回复:

遇到这个问题,应该想想有什么样的软件带这个功能,立刻能想到ifconfig,
然后找一下ifconfig在哪个包里,就会找到net-tools,然后把net-tools包的源码下载下来,
Debian系的Linux下载源码很方便(apt-get source net……
[/Quote]

可以的,ifconfig -a,比如我的无线网卡没有使用,一样能显示MAC地址
wlan0     Link encap:Ethernet  HWaddr 00:24:2c:ad:70:ae  
UP BROADCAST 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)
do_fork 2010-06-18
  • 打赏
  • 举报
回复
遇到这个问题,应该想想有什么样的软件带这个功能,立刻能想到ifconfig,
然后找一下ifconfig在哪个包里,就会找到net-tools,然后把net-tools包的源码下载下来,
Debian系的Linux下载源码很方便(apt-get source net-tools)即可,
然后打开ifconfig.c开始分析源码。

如果分析源码觉得太麻烦,
可以strace ifconfig看一下ifconfig调用了哪些系统调用,
从系统调用很容易分析出ifconfig是如何获得MAC地址的。
ForestDB 2010-06-18
  • 打赏
  • 举报
回复
根据fo_fork的提示,扫了遍ifconfig的代码,发现
所有的接口(的名字),是通过解析/proc/net/dev得到的
然后接口的MAC地址,是通过在接口上调用ioctl(SIOCGIFHWADDR)得到的
craboy1 2010-06-18
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 do_fork 的回复:]

遇到这个问题,应该想想有什么样的软件带这个功能,立刻能想到ifconfig,
然后找一下ifconfig在哪个包里,就会找到net-tools,然后把net-tools包的源码下载下来,
Debian系的Linux下载源码很方便(apt-get source net-tools)即可,
然后打开ifconfig.c开始分析源码。

如果分析源码觉得太麻烦,
可以strace ifco……
[/Quote]

do_fork您还有什么好的调试方法!谈一下!看到你的回复,感觉胜过看一周的贴子
wolflion 2010-06-17
  • 打赏
  • 举报
回复
好像有个结构体的。
可以直接得到的。
xiaowai0219 2010-06-17
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include<unistd.h>

int main(int argc, char *argv[])
{
struct ifreq ifreq;
int sockfd;
unsigned char buffer_mac[6];
if((sockfd=socket(AF_INET,SOCK_STREAM,0))<0){/*创建套接口,后用于获取mac地址*/
perror("sockfd");
return -1;
}
strcpy(ifreq.ifr_name,"eth0"); /*获取一号以太网卡的物理地址*/
if(ioctl(sockfd,SIOCGIFHWADDR,&ifreq)<0){/*获取mac地址*/
perror("ioctl");
return -2;
}
memcpy(buffer_mac,ifreq.ifr_hwaddr.sa_data,6);/*复制mac地址到buffer_mac*/
printf("MAC:%02x:%02x:%02x:%02x:%02x:%02x\n",buffer_mac[0],buffer_mac[1],buffer_mac[2],buffer_mac[3],buffer_mac[4],buffer_mac[5]);
close(sockfd); /*关闭套接口*/
return 0;
}
「已注销」 2010-06-17
  • 打赏
  • 举报
回复
ifconfig?
liutengfeigo 2010-06-17
  • 打赏
  • 举报
回复
百度好像蛮多

69,382

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧