shell script to change MAC address
While I am at it figured I would post this little snippet aswell.
I do realize there are other utilities to spoof the MAC address of devices, but I would rather us my own solution. (plus I was bored).
General Usage:
-h print help
-s generate random mac address and assign it
-u set mac address to what it was from the beginning
ToDo:
- add CLI flag to define interface
- add CLI flag to manually define mac address
- resolve issue with assigning the MAC, ifconfig has issues with certain mac address's not sure why yet.
Download: mediafire.com/download.php?hean10e25zi
Code:
Code:
#!/bin/bash
#
# Synopsis: MAC address spoofing utility
# Author: thims (thims DOT local AT gmail DOT com)
# Version: 0.1
# Date: 20091104
# Comments:
# ToDo:
# - figure out why certain MAC addresss cause ifconfig to exit with error (Error: could not set spoofed MAC address, possibly try running againg)
# Editable variables
iface=wlan0 #desired interface
origMAC=ff:ff:ff:ff:ff:ff #original MAC address
### Code Begins ###
spfMAC=
alph=(0 1 2 3 4 5 6 7 8 9 A B C D E F)
alph_len=${#alph[*]}
function help() {
cat << EOF
Usage: $0 [args]
-h, --help - Print this help and exit
-s, --spoof - Spoof MAC address to a randomly generated address
-u, --unspoof - Return spoofed MAC address to original MAC address
EOF
}
function die() {
if [ -n "$1" ] ;then
echo "$1"
fi
exit 1
}
function genMAC() {
for i in $(seq 6)
do
for x in $(seq 2)
do
spfMAC=${spfMAC}${alph[$((RANDOM % alph_len))]}
done
spfMAC=${spfMAC}:
done
spfMAC=${spfMAC:0:17}
}
function changeIface() {
MAC="$1"
ifconfig $iface &> /dev/null
if [ $? -gt 0 ] ;then
die "Error: IFACE $iface does not exist(possibly down?)"
fi
ifconfig $iface down
ifconfig $iface inet hw ether $MAC up &> /dev/null
# the following if is in place because ifconfig was being picky about certain MAC addresss
if [ $? -gt 0 ] ;then
die "Error: could not set spoofed MAC address, possibly try running againg"
fi
}
if [ $UID -gt 0 ] ;then
die "Error: Sorry dude, gotta be root!"
fi
while [ $# -gt 0 ]
do
case "$1" in
"-h"|"--help")
help
die
;;
"-s"|"--spoof")
genMAC
changeIface $spfMAC
;;
"-u"|"--unspoof")
changeIface $origMAC
;;
*)
help
die
;;
esac
shift
done
All feeback welcome, I would love to hear your thoughts.