最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Arm Linux 修改 网络 mac 地址的方式方法

运维笔记admin2浏览0评论

一、指令修改

查看网络信息指令

ifconfig

修改网络 mac 地址,指令

ifconfig 网卡名 hw  ether mac地址

例如:
ifconfig eth0 hw ether 08:00:27:00:01:96

二、C语言程序修改

1.使用 ioctl 和 SIOCSIFHWADDR 来设置MAC地址,示例代码如下:

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

  
int main(int argc,char *argv[]) 
{  
    int sockfd;  
    struct ifreq ifr;  
    unsigned char new_mac[6] = {0x08, 0x00, 0xc0, 0xa8, 0xec, 0x97}; // 新的MAC地址  
  
    // 创建一个socket用于ioctl调用  
    if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) 
    {  
        perror("socket");  
        exit(EXIT_FAILURE);  
    }  
  
    // 清除ifr结构并设置接口名称  
    memset(&ifr, 0, sizeof(ifr));  
    strncpy(ifr.ifr_name, "eth0", IFNAMSIZ-1); // 设置网络接口名称,比如eth0  
  
    // 设置新的MAC地址  
    ifr.ifr_hwaddr.sa_family = ARPHRD_ETHER;  
    memcpy(ifr.ifr_hwaddr.sa_data, new_mac, 6);  
  
    // 使用ioctl调用设置MAC地址  
    if (ioctl(sockfd, SIOCSIFHWADDR, &ifr) < 0) 
    {  
        perror("ioctl(SIOCSIFHWADDR)");  
        close(sockfd);  
        exit(EXIT_FAILURE);  
    }  
  
    // 关闭socket  
    close(sockfd);  
  
    printf("MAC address changed successfully.\n");  
  
    return 0;  
}

测试效果:

2.使用 shell 指令 进行

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

char ethIfconfigCmd[128]={0};
char *ip="192.168.1.151";
int a,b,c,d;
int main()
{
    int ret;
    sscanf (ip,"%d.%d.%d.%d",&a,&b,&c,&d);
    printf ("%d.%d.%d.%d\n",a,b,c,d);

    memset(ethIfconfigCmd,0,sizeof(ethIfconfigCmd));
    sprintf(ethIfconfigCmd,"ifconfig eth0 hw ether %02x:%02x:%02x:%02x:%02x:%02x",0x08,0x00,0x27,0x00,0x01,0x96);
    ret=system(ethIfconfigCmd);
    //printf("ret=%d\n",ret);
    if (ret != 0) 
    {
        //printf("%d:%s\n",errno,strerror(errno));
        printf("ret:%d,filename:%s,function:%s,lineNum:%d\n",ret,__FILE__,__FUNCTION__,__LINE__);
        //return -1;
    }

     return 0;
}

测试结果如下:

或者

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

char ethIfconfigCmd[128]={0};
char *ip="192.168.1.151";
int a,b,c,d;

char str[2][128]={0};

static int eth_mac_write(char *cmd)
{
    unsigned char i=0;
    FILE *fstream=NULL; 
    char str[2][128]={0};
    if(NULL==(fstream=popen(cmd,"r")))    
    {   
            fprintf(stderr,"execute command failed: %s",strerror(perror));    
            return -1;    
    }
    if(NULL==fgets(str[0], sizeof(str[0]), fstream))  
    {
    }
    else
    {
        printf("%s\r\n",str[0]);
    }
    if(NULL==fgets(str[1], sizeof(str[1]), fstream))  
    {
    }
    else
    {
        printf("%s\r\n",str[1]);
    }
    pclose(fstream);  //关闭 popen
    return 0;
}

int main()
{
    int ret;
    sscanf (ip,"%d.%d.%d.%d",&a,&b,&c,&d);
    printf ("%d.%d.%d.%d\n",a,b,c,d);

    memset(ethIfconfigCmd,0,sizeof(ethIfconfigCmd));
    sprintf(ethIfconfigCmd,"ifconfig eth0 hw ether %02x:%02x:%02x:%02x:%02x:%02x",0x08,0x00,0x27,0x00,0x01,0x98);
    ret=eth_mac_write(ethIfconfigCmd);
    //printf("ret=%d\n",ret);
    if (ret != 0) 
    {
        //printf("%d:%s\n",errno,strerror(errno));
        printf("ret:%d,filename:%s,function:%s,lineNum:%d\n",ret,__FILE__,__FUNCTION__,__LINE__);
        //return -1;
    }

     return 0;
}

执行前

执行后

发布评论

评论列表(0)

  1. 暂无评论