schips

前言:

将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)), "\n"\' | 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

分类:

技术点:

相关文章:

  • 2021-09-18
  • 2021-08-19
  • 2022-12-23
  • 2021-07-03
  • 2021-07-17
  • 2021-04-17
  • 2022-01-16
  • 2022-12-23
猜你喜欢
  • 2021-11-28
  • 2022-02-07
  • 2022-02-07
  • 2021-05-29
  • 2022-12-23
  • 2022-01-17
  • 2022-12-23
相关资源
相似解决方案