I think this is an important tool to have so I made one for myself and for anybody else who might want to use it.
#!/bin/bash
##
### mactool.sh - set random/designated/original mac address
## Note: run as root
#
export MAC
export OLDMAC
export RANDMAC
export NEWMAC=$3
export TMP='/tmp/stored.mactool'
export IFACE=$2
export BASE16=('a' 'b' 'c' 'd' 'e' 'f' 0 1 2 3 4 5 6 7 8 9)
usage() {
echo """
mactool.sh - manipulate MAC address
by frog on June 1st, 2012
Usage:
Set MAC: $0 -s <interface> <mac>
Set Random MAC: $0 -r <interface>
Set Original MAC: $0 -o <interface>
Show Current MAC: $0 -w <interface>
Example: $0 -s wlan1 00:11:22:aa:bb:cc
"""
exit
}
showMAC() {
MAC=`/sbin/ifconfig $IFACE | grep HW`
MAC=`echo ${MAC:38:18}`
echo "[+] MAC address for" $IFACE":" $MAC
}
saveOldMAC() {
MAC=`ifconfig $IFACE | grep HW`
MAC=`echo ${MAC:38:18}`
if [ -a $TMP ]; then
if [ `cat $TMP` != "$MAC" ]; then
return
fi
fi
OLDMAC=`ifconfig $IFACE | grep HW`
OLDMAC=`echo ${OLDMAC:38:18}`
echo "[-] Saving original MAC:" $OLDMAC "to" $TMP
echo $OLDMAC > $TMP
}
setNewMAC() {
echo "[-] Setting MAC address to" $NEWMAC
ifconfig $IFACE down
ifconfig $IFACE hw ether `echo $NEWMAC | sed 's/://g'`
ifconfig $IFACE up
}
setRandomMAC() {
for(( x=0; x<12; x++ )); do
RANDMAC[$x]=`shuf -e ${BASE16[@]} -n 1`
done
RANDMAC=`echo ${RANDMAC[@]} | sed 's/ //g'`
echo "[-] Setting Random MAC:" $RANDMAC
ifconfig $IFACE down
ifconfig $IFACE hw ether $RANDMAC 2> /dev/null
if [ $? -gt 0 ]; then
setRandomMAC
fi
ifconfig $IFACE up
}
restoreOldMAC() {
if [ -z `cat $TMP 2> /dev/null` ]; then
echo "[+] MAC address is original"
exit
fi
echo "[-] Restoring MAC address to" `cat $TMP`
ifconfig $IFACE down
ifconfig $IFACE hw ether `cat $TMP | sed 's/://g'`
ifconfig $IFACE up
}
checkForRoot() {
# Make sure we are root(needed to manipulate interfaces)
if [ `whoami` != 'root' ]; then
echo "[-] You need to be root; try again.."
exit
fi
}
checkArgIface() {
# Check for interface as argument 2
if [ -z $IFACE ]; then
echo "[-] Error: Set network interface"
usage
exit
fi
}
checkArgMAC() {
# Check for mac to set as argument 3
if [ -z $NEWMAC ]; then
echo "[-] Error: Set MAC address"
usage
exit
fi
}
cleanup() {
echo "[-] Deleting" $TMP
rm -f $TMP
}
#
##
### Start
# Check our arguments meticulously..
if [ -z $1 ]; then
usage
exit
fi
# Check for option to set mac
if [ $1 = '-s' ]; then
checkForRoot
checkArgIface
checkArgMAC
saveOldMAC
setNewMAC
showMAC
exit
fi
# Check for option to set random mac
if [ $1 = '-r' ]; then
checkForRoot
checkArgIface
saveOldMAC
setRandomMAC
showMAC
exit
fi
# Check for option to set mac to original
if [ $1 = '-o' ]; then
checkForRoot
checkArgIface
restoreOldMAC
cleanup
showMAC
exit
fi
# Check for option to show mac
if [ $1 = '-w' ]; then
checkArgIface
showMAC
exit
fi
### End
##
#