Created
March 24, 2017 07:58
-
-
Save jsfaint/f53cfa0b8c94303692447632efba341a to your computer and use it in GitHub Desktop.
Update MAC address using ethtool
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
#!/bin/bash | |
PATTERN='^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$' | |
print_usage() { | |
echo "Need NIC name" | |
echo "usage: $0 eth0 00:11:22:33:44:55" | |
} | |
if [ -z $1 ]; then | |
print_usage $0 | |
exit 1 | |
else | |
dev=$1 | |
fi | |
if [ -z $2 ]; then | |
print_usage $0 | |
exit 1 | |
else | |
mac=$2 | |
fi | |
# Check MAC address | |
ethtool $dev > /dev/null | |
if [[ $? -ne 0 ]]; then | |
echo "Invalid NIC name $dev" | |
print_usage $0 | |
exit 1 | |
fi | |
# Check MAC address | |
if [[ ! "$mac" =~ $PATTERN ]]; then | |
echo "Invalid mac address" | |
print_usage $0 | |
exit 1 | |
fi | |
# Update MAC address | |
for ((i=0; i<6; i++)) | |
do | |
tmp=`expr $i + 1` | |
val=`echo $mac | awk -F ':' -v "v=$tmp" '{print $v}'` | |
ethtool -E $dev magic 0x10d38086 offset $i value 0x$val | |
done | |
echo "Update MAC address complete." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment