从硬件本身查找原始 MAC 地址 [英] Finding original MAC address from Hardware itself
问题描述
Os:REDHAT LINUX Linux 管理:2.6.18.8-1 #
Os:REDHAT LINUX Linux manage: 2.6.18.8-1 #
可以直接从网卡读取MAC地址吗?我有下面的代码,但它只是从上面的层读取而不是卡本身!!!
我想弄清楚如何在我的 linux 机器上找到以太网 NIC 的原始 MAC 地址.我了解如何使用 ifconfig 查找当前 MAC 地址,但是如果地址已更改,请说使用'ifconfig eth0 hw ether uu:vv:ww:yy:xx:zz'
,或者我使用 vi/etc/sysconfig/network-scripts/ifcfg-eth0
.this 文件...我也可以在 REBOOT
中成功 UP
它.我如何找到原件?一定有办法找到它,因为它仍然永久烧入卡中,但我找不到读取烧入地址的工具.是否有任何实用程序或命令?我想为它编写C代码.但不知道如何在上述情况下做到这一点.
I'm trying to figure out how to find the original MAC address of an ethernet NIC on my linux box. I understand how to find the current MAC address using ifconfig, but if the address has been changed, say by using
'ifconfig eth0 hw ether uu:vv:ww:yy:xx:zz'
,or I set "permanent" it using vi /etc/sysconfig/network-scripts/ifcfg-eth0
.this file...I can successfully UP
it in REBOOT
also. how do I find the original? There must be a way to find it, because it is still burned permanently into the card, but I can't find a tool to read the burned in address.
is there any utility for it or command for it?
I suppose to write C code for it. but don't know how to do this in above case.
** 下面的代码给出了我的当前 MAC 但不是原始 MAC
** below code gives my current MAC but not original MAC
#include <stdio.h> /* Standard I/O */
#include <stdlib.h> /* Standard Library */
#include <errno.h> /* Error number and related */
#define ENUMS
#include <sys/socket.h>
#include <net/route.h>
#include <net/if.h>
#include <features.h> /* for the glibc version number */
#if __GLIBC__ >= 2 && __GLIBC_MINOR >= 1
#include <netpacket/packet.h>
#include <net/ethernet.h> /* the L2 protocols */
#else
#include <asm/types.h>
#include <linux/if_packet.h>
#include <linux/if_ether.h> /* The L2 protocols */
#endif
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/un.h>
#include <sys/ioctl.h>
#include <netdb.h>
int main( int argc, char * argv[] ){
unsigned char mac[IFHWADDRLEN];
int i;
get_local_hwaddr( argv[1], mac );
for( i = 0; i < IFHWADDRLEN; i++ ){
printf( "%02X:", (unsigned int)(mac[i]) );
}
}
int get_local_hwaddr(const char *ifname, unsigned char *mac)
{
struct ifreq ifr;
int fd;
int rv; // return value - error value from df or ioctl call
/* determine the local MAC address */
strcpy(ifr.ifr_name, ifname);
fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
if (fd < 0)
rv = fd;
else {
rv = ioctl(fd, SIOCGIFHWADDR, &ifr);
if (rv >= 0) /* worked okay */
memcpy(mac, ifr.ifr_hwaddr.sa_data, IFHWADDRLEN);
}
return rv;
}
推荐答案
当然在 ethtool 3.1 中你可以只打印地址:ethtool -P|--show-permaddr DEVNAME 显示永久硬件地址
Certainly in ethtool 3.1 you can just print the address: ethtool -P|--show-permaddr DEVNAME Show permanent hardware address
例如
ethtool -P eth0
永久地址:94:de:80:6a:21:25
Permanent address: 94:de:80:6a:21:25
这篇关于从硬件本身查找原始 MAC 地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!