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

1.使用$RANDOM和md5sum

MACFILE=/etc/config/mac
ETHNAME=eth0

if [ ! -f "$MACFILE" ]; then
 # Create HEX code to FILE (使其以88开头)
 echo 88`echo $RANDOM | md5sum | sed 's/\(..\)/&/g' | cut -c1-10` > $MACFILE
fi

# Set mac
/sbin/ifconfig $ETHNAME down
/sbin/ifconfig $ETHNAME hw ether `cat $MACFILE`
/sbin/ifconfig $ETHNAME up

2.使用openssl工具

MACFILE=/etc/config/mac
ETHNAME=eth0

if [ ! -f "$MACFILE" ]; then
 # Create HEX code to FILE
 echo 88`openssl rand -hex 6 |sed 's/\(..\)/&/g;s/:$//'    | cut -c1-12` > $MACFILE
fi

# Set mac
/sbin/ifconfig $ETHNAME down
/sbin/ifconfig $ETHNAME hw ether `cat $MACFILE`
/sbin/ifconfig $ETHNAME up

3.使用perl命令   

MACFILE=/etc/config/mac
ETHNAME=eth0

if [ ! -f "$MACFILE" ]; then
 # Create HEX code to FILE
 echo 88`perl -e 'print join("",map{sprintf "%0.2x",rand(256)}(1..6)), "\n"' | cut -c1-10` > $MACFILE
fi

# Set mac
/sbin/ifconfig $ETHNAME down
/sbin/ifconfig $ETHNAME hw ether `cat $MACFILE`
/sbin/ifconfig $ETHNAME up

猜你喜欢

转载自www.cnblogs.com/schips/p/12081464.html