Openwrt 更新 MAC地址脚本

起因校园网会自动拉黑MAC地址导致无法DHCP获取到IP,只能通过更换wan口的mac地址就可以获取新IP

#!/bin/sh
newMac() {
    # oldIP=$(ubus call network.interface.wan status | grep \"address\" | grep -oE '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}')
    oldIP=$(ifconfig wan | grep -o -E "inet addr:[0-9.]+" | grep -oE '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}')
    oldMac=$(ifconfig wan | grep -o -E '([[:xdigit:]]{1,2}:){5}[[:xdigit:]]{1,2}')
    printf 'Old : %-15s = > %s\n' "$oldMac" "$oldIP"
    newMAC=$(dd if=/dev/urandom count=1 2>/dev/null | md5sum | sed 's/^\(.\)\(..\)\(..\)\(..\)\(..\)\(..\).*$/\14:\2:\3:\4:\5:\6/g')
    # echo "New : "$newMAC
    ifconfig wan down
    ifconfig wan hw ether $newMAC
    ifconfig wan up
    sleep 15s
    # newIP=$(ubus call network.interface.wan status | grep \"address\" | grep -oE '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}')
    newIP=$(ifconfig wan | grep -o -E "inet addr:[0-9.]+" | grep -oE '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}')
    newMac=$(ifconfig wan | grep -o -E '([[:xdigit:]]{1,2}:){5}[[:xdigit:]]{1,2}')
    printf 'New : %-15s = > %s\n' "$newMac" "$newIP"
}

ifNewMac() {
    oldIP=$(ifconfig wan | grep -o -E "inet addr:[0-9.]+" | grep -oE '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}')
    oldMac=$(ifconfig wan | grep -o -E '([[:xdigit:]]{1,2}:){5}[[:xdigit:]]{1,2}')
    printf '%-15s = > %s\n' "$oldMac" "$oldIP"
    if [ -z $oldIP ]; then
        clear
        newMac
    fi
}

showMac() {
    IP=$(ifconfig wan | grep -o -E "inet addr:[0-9.]+" | grep -oE '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}')
    Mac=$(ifconfig wan | grep -o -E '([[:xdigit:]]{1,2}:){5}[[:xdigit:]]{1,2}')
    printf '%-15s = > %s\n' "$Mac" "$IP"
}


case $1 in
"new")
    newMac
    ;;
"if")
    ifNewMac
    ;;
"show")
    showMac
    ;;
*)
    ifNewMac
    ;;
esac

赞 (0)

评论区

发表评论

49+16=?

暂无评论,要不来一发?

回到顶部