Skip to content

Instantly share code, notes, and snippets.

@gleuch
Last active March 28, 2017 23:12
Show Gist options
  • Save gleuch/6a7a2011b585fe168a54 to your computer and use it in GitHub Desktop.
Save gleuch/6a7a2011b585fe168a54 to your computer and use it in GitHub Desktop.
Alter your MAC address when connecting to WiFi networks. For OS X (Macbooks, etc.)
#
# MACchange
# v1.1
#
# Alter your MAC address when connecting to WiFi networks.
#
# -----------------------------------------------------------------------------
#
# - Works great for places that have 1 hour time limits on wifi access, as
# tracked by mac address
# - Avoid constant detection when regularly connecting to the same network
# - Sometimes fixes network registrations when connecting or after being booted
#- from a network
#
#
# Installation:
# -----------------------------------------------------------------------------
#
# 1. Download into your home folder (/Users/[login]/) or whichever folder you
# prefer to run this file.
#
# 2. Allow execution of script: (change directory as needed)
# chmod +x ~/macchange.sh
#
# 3. Add an alias in your .bash_profile: (change directory as needed)
# echo "alias macchange=\"sudo ~/macchange.sh\"" >> ~/.bash_profile
#
# 4. Re-source your bash profile in your terminalwindow
# source ~/.bash_profile
#
# 5. Run as necssary in your terminal, enter password if prompted for sudo
# macchange
#
#
# Configration:
# -----------------------------------------------------------------------------
#
# The network interface for most newer Macbooks (without an ethernet port) are
# `en0`. If you are using an iMac, Mac Pro, or a Macbook with an ethernet port,
# change the `INTF` variable to `en1` and test to ensure you are using the
# correct interface for your WiFi network card.
#
#
# -----------------------------------------------------------------------------
# Created from existing code available on stackoverflow and other place.
# Remixed into shell script w/ instructions by Greg Leuch <https://gleu.ch>
#
# -----------------------------------------------------------------------------
# Updated 28 Mar 2017
# Added counter to reset it a few times before committing, and check that new
# MAC address was properly set before restarting WiFi.
#
# -----------------------------------------------------------------------------
# -----------------------------------------------------------------------------
# -----------------------------------------------------------------------------
# Number of unique sets
COUNTER=2
# WiFi interface -- new Macbooks are just en0 for wifi. if you have an ethernet
# port, then this might likely be en1.
INTF=en0
# Yes'm
INTM=ether
# -----------------------------------------------------------------------------
# Get current MAC address
MAC_ORIG=`ifconfig $INTF | grep $INTM | sed 's/ ether //'`
echo "Running on interface $INTF $INTM:"
echo " - Old MAC: $MAC_ORIG"
# Loop
until [ $COUNTER -lt 0 ]; do
# Generate new MAC address
MAC_NEW=`openssl rand -hex 6 | sed 's/\(..\)/\1:/g; s/.$//'`
# Set new MAC address
sudo ifconfig $INTF $INTM $MAC_NEW
# Check newly set address, incremenet counter if matches
MAC_NOW=`ifconfig $INTF | grep $INTM | sed 's/ ether //'`
if [ $MAC_NOW != $MAC_NEW ]
then
let COUNTER-=1
fi
done
# MAC successfully set
echo " - New MAC: $MAC_NOW"
# Restart WiFi for MAC address to take effect
echo "Restarting network on $INTF $INTM...\c"
networksetup -setairportpower $INTF off
sleep 1
networksetup -setairportpower $INTF on
echo "done!"
# -----------------------------------------------------------------------------
# -----------------------------------------------------------------------------
# -----------------------------------------------------------------------------
@gleuch
Copy link
Author

gleuch commented Nov 3, 2014

Hoping to add an option in v2 that remembers and reconnects to the WiFi network you are on instead of possibly connecting to a previously existing one that might be in your area.

@gleuch
Copy link
Author

gleuch commented Mar 28, 2017

Updated to v1.1 to have it check that new MAC address correctly sets, as sometimes it does not seem to take on first pass. Additional option to have it attempt to generate multiple times through $COUNTER loop.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment