从硬件本身查找原始MAC地址 [英] Finding original MAC address from Hardware itself

查看:399
本文介绍了从硬件本身查找原始MAC地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Os:REDHAT LINUX Linux管理:2.6.18.8-1#

Os:REDHAT LINUX Linux manage: 2.6.18.8-1 #

可以直接从NIC读取MAC地址吗?我有下面的代码,但它只是从上面的层读取,而不是卡本身!!!

我试图弄清楚如何在我的Linux机器上找到以太网NIC的原始MAC地址.我了解如何使用ifconfig查找当前的MAC地址,但是如果地址已更改(例如通过使用 'ifconfig eth0 hw ether uu:vv:ww:yy:xx:zz',或者我使用vi /etc/sysconfig/network-scripts/ifcfg-eth0设置为永久".此文件...我也可以在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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆