Skip to content

Instantly share code, notes, and snippets.

@korc
Last active November 30, 2017 13:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save korc/a847ff5d9517d457d2d15c851c865660 to your computer and use it in GitHub Desktop.
Save korc/a847ff5d9517d457d2d15c851c865660 to your computer and use it in GitHub Desktop.
#!/bin/sh
# Changes WiFi NIC MAC address to MD5-based semi-random value
name="$1"
iface="$2"
: "${ifaces:=$(for i in /sys/class/net/*;do test ! -d $i/wireless || { echo -n "$_s${i##*/}";_s=" ";};done)}"
: "${iface:=$(echo "$ifaces" | cut -f1 -d" ")}"
test -n "$iface" || {
echo "ERROR: cannot find interface" >&2
test -z "$name" || exit 1
}
test -n "$name" || {
echo "Usage: ${0##*/} <name> [<iface=$iface>]" >&2
echo "Available interaces: $ifaces" >&2
exit 1
}
name2mac() {
local name="$1" new_mac octet
for octet in $(echo -n "$name" | md5sum | fold -w2 | head -6);do
if test -z "$new_mac";then
new_mac="$(printf "%02x" $((0x$octet&0xfe|2)))"
else
new_mac="$new_mac:$octet"
fi
done
echo "$new_mac"
}
new_mac="$(name2mac "$name")"
set -xe
ip link set "$iface" address "$new_mac"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment