Skip to content

Instantly share code, notes, and snippets.

@mislav
Last active August 29, 2015 13:55
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mislav/8724668 to your computer and use it in GitHub Desktop.
Save mislav/8724668 to your computer and use it in GitHub Desktop.
Randomize MAC address (e.g. unlimited wifi access on airports)
#!/bin/bash
set -e
current_mac() {
ifconfig en0 ether | awk '/ether / { print $2 }'
}
current_mac_wifi() {
networksetup -getmacaddress Wi-Fi | awk '{ print $3 }'
}
random_mac() {
openssl rand -hex 6 | sed "s/\(..\)/\1:/g; s/:$//"
}
assign_mac() {
sudo ifconfig en0 ether "$1"
}
randomize() {
old_mac="$(current_mac)"
echo "previous MAC address: ${old_mac}"
# The first byte of a host MAC address must be even
while [ "$old_mac" = "$(current_mac)" ]; do
assign_mac "$(random_mac)"
sleep 0.1
done
echo "new MAC address: $(current_mac)"
}
networksetup -setairportpower en0 on
sudo /System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport -z
if [ -n "$1" ]; then
assign_mac "$1"
echo "new MAC address: $(current_mac)"
else
randomize
fi
sudo networksetup -detectnewhardware
echo "Wi-Fi MAC address: $(current_mac_wifi)"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment