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