Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
MAC Address Switcher v1.1.0
#!/bin/bash
# MAC Address Switcher v1.1.0
# Written by lesander - http://github.com/lesander
# Licensed under GPLv3
# Tested on Ubuntu 14.04
# - Set MAC addresses and settings.
address1=""
address2=""
network=""
networks=$(ls /sys/class/net | tr '\n' '/' | sed '$s/ $/\n/')
networks=${networks%?}
# - Check if addresses are set.
if [[ $address1 == "" || $address2 == "" || $network == "" || $1 == "settings" ]]; then
# Determine the case.
if [ $1 == "settings" ]; then
echo "[*] MAC Address Switcher - Change Settings"
else
echo "[!] Missing address(es) or network in configuration."
fi
# Pick a network.
read -p "[?] Which network would you like to use? [$networks] "
newnetwork=$REPLY
# Get address1 (current mac).
current=$(cat /sys/class/net/$newnetwork/address)
newaddress1=$current
echo "[*] Your current MAC address for network $newnetwork is $current."
# Ask for address2.
read -p "[?] Please enter the MAC address you want to switch to / from: "
newaddress2=$REPLY
# Save to this file.
sed -i -e '0,/address1="'$address1'"/s//address1="'$newaddress1'"/' ${0}
sed -i -e '0,/address2="'$address2'"/s//address2="'$newaddress2'"/' ${0}
sed -i -e '0,/network="'$network'"/s//network="'$newnetwork'"/' ${0}
echo "[!] Saved addresses and network to file."
exit
fi
# - Get current MAC address.
current=$(cat /sys/class/net/$network/address)
# - Print info.
echo "[*] MAC Address Switcher v1.1.0"
echo "[*] Current MAC address for network $network is '$current'."
# - Make sure we're running as root.
echo "[!] This program needs sudo permissions to alter the system's MAC address."
sudo -v
if [ $EUID == "0" ]; then
echo "[!] Please run this program as sudo."
exit 2
fi
# - Disable the network.
echo "[*] Disabling network $network."
sudo ifconfig $network down
# - Switch MAC address.
if [ $current == $address1 ]; then
echo "[*] Switching to MAC address 2 $address2."
sudo ifconfig $network hw ether $address2
elif [ $current == $address2 ]; then
echo "[*] Switching to MAC address 1 $address1."
sudo ifconfig $network hw ether $address1
else
echo "[*] Current MAC address is neither of the saved addresses."
fi
# - Re-enable network.
echo "[*] Re-enabling network $network."
sleep 3
sudo ifconfig $network up
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.