Linux中 CPU 引出网卡的 MAC 地址的设置

  CPU直接引出的网卡地址,不能用ethool操作,它不同于英特尔的网卡,英特尔的网卡可以直接把MAC地址烧录在网卡的物理内存,所以可以用ethtool操作。那CPU的网卡MAC地址如何固定呢,思路就是把MAC地址写在EEPROM里。然后系统开机的时候读取EEPROM的MAC地址数据,执行指令更新

  1、编写脚本把MAC保存到EEPROM

vim eth2SetMacAddrCPU.sh
------------------------------------------------------------------
#!/bin/sh
if [ $# -eq 6 ]
then
        echo "start set mac addr"
        i2cset -f -y 3 0x50 0x01 0x$1
        sync
        i2cset -f -y 3 0x50 0x02 0x$2
        sync
        i2cset -f -y 3 0x50 0x03 0x$3
        sync
        i2cset -f -y 3 0x50 0x04 0x$4
        sync
        i2cset -f -y 3 0x50 0x05 0x$5
        sync
        i2cset -f -y 3 0x50 0x06 0x$6
        sync
        UpMacEth2.sh  #更新MAC地址脚本,在MAC写入到EEPROM后就直接更新,这样就不用重启也可以更新MAC地址了
else
        echo "paramenter number error"
fi

  2、编写脚本更新MAC地址

vim UpMacEth2.sh
------------------------------------------------------------------------------------
#/bin/sh
Mac1=$(i2cget -f -y 3 0x50 0x01)
Mac2=$(i2cget -f -y 3 0x50 0x02)
Mac3=$(i2cget -f -y 3 0x50 0x03)
Mac4=$(i2cget -f -y 3 0x50 0x04)
Mac5=$(i2cget -f -y 3 0x50 0x05)
Mac6=$(i2cget -f -y 3 0x50 0x06)

GetMac1=${Mac1:2:2}
GetMac2=${Mac2:2:2}
GetMac3=${Mac3:2:2}
GetMac4=${Mac4:2:2}
GetMac5=${Mac5:2:2}
GetMac6=${Mac6:2:2}

Mac=$GetMac1:$GetMac2:$GetMac3:$GetMac4:$GetMac5:$GetMac6
echo "Up eth2 MAC is = $Mac"
ifconfig eth2 down ifconfig eth2 hw ether $Mac #更新MAC地址 ifconfig eth2 up

  3、把更新MAC地址的脚本做成开机自启动程序

  这样就可以每次开机时自动跟新MAC地址了。
 
posted @ 2022-11-17 14:56  白菜没我白  阅读(250)  评论(0编辑  收藏  举报