-
-
Save moonpfe/61e290113eb3b35458df49dd87a7d914 to your computer and use it in GitHub Desktop.
set mac address depending on device name
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
setup_mac_address() { | |
local device=$1 | |
local mac=$(ip link show $device | grep "link/ether\b" | awk '{print $2}') | |
[ "$mac" = "00:00:00:00:00:00" ] || return | |
local interfaces=`rtcap -l` | |
if [ "$interfaces" = "" ]; then | |
return | |
fi | |
# eth0 : F4:49:EF:F0:00:00 | |
# eth1 : F4:49:EF:F0:01:00 | |
# enp1s0 : F4:49:EF:F1:01:00 | |
# enp2s1 : F4:49:EF:F1:02:01 | |
# enp6s0 : F4:49:EF:F1:06:00 | |
# eno1s0 : F4:49:EF:F2:01:00 | |
for iface in $interfaces | |
do | |
if [ "$iface" == "$device" ]; then | |
local ids=("0" "0" "0") | |
case $device in | |
eth*) ids[0]="0";; | |
enp*) ids[0]="1";; | |
*) ids[0]="2";; | |
esac | |
local index=1 | |
numbers=$(echo $device | grep -o -E '[0-9]+') | |
for id in $numbers | |
do | |
ids[$index]=$id | |
index=$(($index + 1)) | |
[ $index -gt 2 ] && break | |
done | |
local ID0=${ids[0]:${#ids[0]}-1:1} | |
local ID1=${ids[1]:${#ids[1]}-2:2} | |
local ID2=${ids[2]:${#ids[2]}-2:2} | |
/usr/bin/ip link set dev $device address $(printf "F4:49:EF:F%01d:%02d:%02d\n" $ID0 $ID1 $ID2) | |
/usr/bin/ip link set dev $device up | |
break | |
fi | |
done | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment