Last active
April 17, 2023 20:46
-
-
Save JJTech0130/6036ab3dcd21446f641b458cf10f4392 to your computer and use it in GitHub Desktop.
Quick script to change your MAC address
This file contains hidden or 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
# Make sure the script is run as root | |
# Otherwise, try to run it with sudo | |
if [ $EUID -ne 0 ]; then | |
echo "This script must be run as root" | |
sudo $0 | |
exit $? # Exit with the same exit code as sudo | |
fi | |
# Look for .mac files in the current folder | |
shopt -s nullglob | |
macfiles=(*.mac) | |
# If no .mac files are found, exit | |
if [ ${#macfiles[@]} -eq 0 ]; then | |
echo "No .mac files found" | |
exit 1 | |
fi | |
# If more than one .mac file is found, let the user choose | |
if [ ${#macfiles[@]} -gt 1 ]; then | |
echo "Multiple .mac files found, please choose one:" | |
select macfile in "${macfiles[@]}"; do | |
break | |
done | |
else | |
macfile=${macfiles[0]} | |
fi | |
# Read the MAC address from the file | |
mac=$(cat $macfile) | |
# Make it lowercase | |
mac=$(echo $mac | tr '[:upper:]' '[:lower:]') | |
# Get the current MAC address | |
current_mac=$(ifconfig en0 | grep ether | awk '{print $2}') | |
# If the current MAC address is the same as the one in the file, exit | |
if [ "$mac" == "$current_mac" ]; then | |
echo "Current MAC address is already $mac" | |
exit 0 | |
fi | |
# Tell the user what's going on | |
echo "Changing MAC address from $current_mac to $mac" | |
# Turn the interface off and on again to disconnect from the network | |
#ifconfig en0 down | |
#ifconfig en0 up | |
# Disassociate from the current network | |
/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport "en0" -z | |
# Wait for a bit to make sure the interface is ready | |
sleep 1 | |
# Change the MAC address | |
ifconfig en0 ether $mac | |
# Make sure the change was successful | |
verify_mac=$(ifconfig en0 | grep ether | awk '{print $2}') | |
if [ "$mac" != "$verify_mac" ]; then | |
echo "Failed to change MAC address" | |
exit 1 | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment