-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathchangeMAC.sh
77 lines (68 loc) · 1.93 KB
/
changeMAC.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/bin/bash
# Change Wifi interface MAC address on macOS >= 10.10
# Because there are times you want to change your wifi interface's MAC address ;)
# By Sebastien Jeanquier (@securitygen)
# Security Generation - http://www.securitygeneration.com
#
# -------------------------------------------------------------
function usage {
echo "Description: Set supplied or randomised Airport (Wifi) MAC address on en0."
echo "Usage: sudo $0 [-r] {MAC address}"
echo " Optional -r: Sets randomised MAC address"
echo " Otherwise sets supplied MAC address."
echo " MAC address is reset to default upon reboot."
echo " Must be run as root."
}
if [[ $# -eq 0 ]] || [ "$1" == "-h" ];
then
usage "$0";
exit 1
elif [[ $EUID -ne 0 ]]; then
echo "[Error] Must be run as root."
exit 1
else
# Read flags
rflag=false
while getopts r opt
do
case "$opt" in
r)
rflag=true
;;
\?) # unknown flag
usage "$0"
exit 1
;;
esac
done
shift "$((OPTIND-1))"
# Get new MAC address
if [[ $rflag == true ]]; then
# Generate random MAC
newMAC=$(openssl rand -hex 6 | sed 's/\(..\)/\1:/g; s/./0/2; s/.$//')
randomised=" (random)"
else
# Check and use supplied MAC address
newMAC=$1
fi
# Check resulting MAC address is valid
re="^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$"
if ! [[ $newMAC =~ $re ]] ; then
echo "Error: new MAC address must be in format 00:aa:11:bb:22:cc (case insensitive), you entered: $newMAC" >&2; exit 1
exit 1
fi
# Confirmation
read -r -p "Are you sure you want to set your MAC address to: $newMAC$randomised? (y/N): " selection
if [[ "$selection" != "y" ]]; then
echo "Bailing out!"
exit 0
fi
# Set new MAC address
# 1) Disable airport interface
sudo /System/Library/PrivateFrameworks/Apple80211.framework/Resources/airport -z
# 2) Change MAC addess on en0
sudo ifconfig en0 ether "$newMAC"
# 3) Re-enable airport interface
networksetup -detectnewhardware
echo "[Done] Set $newMAC on en0."
fi