Skip to content

Instantly share code, notes, and snippets.

@mislav
Last active September 5, 2024 01:46
Show Gist options
  • Select an option

  • Save mislav/8724668 to your computer and use it in GitHub Desktop.

Select an option

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