shell MAC 地址 校验

/***************************************************************************************
 *                            shell MAC 地址 校验
 * 说明:
 *     要对MAC地址进行校验,记录一下正则表达式写法,有些方法在PC上验证是可行的,但到了
 * 嵌入式产品上,可能就不一定能运行了。
 *
 *                                                2016-11-15 深圳 南山平山村 曾剑锋
 **************************************************************************************/

一、参考文档:
    1. BASH regex match MAC address
        http://stackoverflow.com/questions/19959537/bash-regex-match-mac-address
    2. Best way to extract MAC address from ifconfig's output
        http://stackoverflow.com/questions/245916/best-way-to-extract-mac-address-from-ifconfigs-output

二、测试shell脚本如下:
    macPath=/sys/bus/i2c/devices/3-0050/eeprom
    mac=`grep -o -E '([[:xdigit:]]{1,2}:){5}[[:xdigit:]]{1,2}' $macPath`

    if [ ${#mac} -eq 17 ]; then
        echo "MAC Address: $mac"
        ifconfig eth0 hw ether $mac
    else 
        ifconfig eth0 hw ether 70:b3:d5:10:6f:90
    fi

 

posted on 2016-11-15 19:38  zengjf  阅读(1594)  评论(0编辑  收藏  举报

导航