Created
November 9, 2019 03:34
-
-
Save jasonrig/dfcc9cfa85da9b94188a0c3b8851ab90 to your computer and use it in GitHub Desktop.
A shell script to toggle NIC MAC addresses between the factory default and a new random one
This file contains 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
#!/bin/bash | |
if [ "$#" -ne 1 ]; then | |
echo "Network device must be specified" | |
exit 1 | |
fi | |
DEVICE="$1" | |
OLD_MAC=$(ifconfig | grep "$DEVICE:" -A3 | grep ether | head -1 | cut -d ' ' -f 2) | |
NEW_MAC=$(openssl rand -hex 6 | sed 's/\(..\)/\1:/g;s/.$//') | |
if [ -z "$OLD_MAC" ]; then | |
echo "Invalid network device specified" | |
exit 1 | |
fi | |
CONFIG_FILE="$HOME/.mac_address_$DEVICE" | |
if [ ! -f "$CONFIG_FILE" ]; then | |
echo "$OLD_MAC" > "$CONFIG_FILE" | |
ORIGINAL_MAC="$OLD_MAC" | |
else | |
ORIGINAL_MAC=$(cat "$CONFIG_FILE") | |
fi | |
if [ "$OLD_MAC" == "$ORIGINAL_MAC" ]; then | |
echo Setting new mac address: "$NEW_MAC" | |
sudo ifconfig $DEVICE ether "$NEW_MAC" | |
else | |
echo Resetting mac to original: "$ORIGINAL_MAC" | |
sudo ifconfig $DEVICE ether "$ORIGINAL_MAC" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment