O

Openwrt 更新 MAC地址脚本

Bejix 代码笔记 2023-03-06

起因校园网会自动拉黑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
PREV
golang ping go-ping库 简介
NEXT
校园网自动认证

评论(0)

发布评论