Linux 生成随机mac地址,并固化到本地

前言:

将Mac地址随机化并固化到本地可以有效避免同一个网络内,mac地址冲突导致的网络阻塞问题。

以下是有关的方法:

##
#    Copyright By Schips, All Rights Reserved
#    https://gitee.com/schips/
#    File Name:  setMac.sh
#    Created  :  Mon Dec 23
##
#!/bin/sh

# 保存的配置文件
MACFILE=/etc/config/mac
ETHNAME=eth0
# 注: 如果不需要mac以88开头,则删除88,并将cut -cl-10 改为 cut -cl-12
makeMacByMd5() {
    #使用$RANDOM和md5sum(嵌入式无需移植其他软件的优秀可选方案)
    echo 88`echo $RANDOM | md5sum | sed 's/(..)/&/g' | cut -c1-10`
}

makeMacBySSL() {
    #使用openssl工具
    echo 88`openssl rand -hex 6 |sed 's/(..)/&/g;s/:$//' | cut -c1-10`
}

makeMacByPerl() {
    #使用perl命令
    echo 88`perl -e 'print join("",map{sprintf "%0.2x",rand(256)}(1..6)), "
"' | cut -c1-10`
}

# 检查配置是否存在, 否则生成MAC地址
if [ ! -f "$MACFILE" ]; then
 # Create HEX code to FILE (使其以88开头)
    echo `makeMacByMd5`  > $MACFILE
    #echo `makeMacBySSL`  > $MACFILE
    #echo `makeMacByPerl` > $MACFILE
fi


# Set mac
/sbin/ifconfig $ETHNAME down
/sbin/ifconfig $ETHNAME hw ether `cat $MACFILE`
/sbin/ifconfig $ETHNAME up
原文地址:https://www.cnblogs.com/schips/p/12995293.html