-
Notifications
You must be signed in to change notification settings - Fork 0
/
randomize_mac_address.fish
55 lines (49 loc) · 1.73 KB
/
randomize_mac_address.fish
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
function randomize_mac_address
if ! type -q openssl
echo "Error: openssl command not found" >&2
return 1
end
sudo -v
if [ $status -ne 0 ]
echo "Error: Failed to obtain sudo privileges" >&2
return 1
end
# sudo /System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport -z
sudo networksetup -setnetworkserviceenabled Wi-Fi off
sudo networksetup -setnetworkserviceenabled Wi-Fi on
if [ -z $argv[1] ]
set count 0
while true
set macaddress (openssl rand -hex 6 | sed 's/\(..\)/\1:/g; s/.$//')
sudo ifconfig en0 ether $macaddress 2>/dev/null
if [ $status -eq 0 ]
break
else
set count (math $count + 1)
if [ $count -eq 10 ]
echo "Error: Too many failed attempts to set MAC address" >&2
return 1
end
end
end
else
set macaddress $argv[1]
if ! string match -r -q '([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})' $macaddress
if string match -r -q '([0-9A-Fa-f]{2}){6}' $macaddress
set macaddress (echo $macaddress | sed 's/\(..\)/\1:/g; s/.$//')
else
echo "Error: Invalid MAC address format" >&2
return 1
end
end
sudo ifconfig en0 ether $macaddress
if [ $status -ne 0 ]
echo "Error: Failed to set MAC address to $macaddress" >&2
return 1
end
end
set -x -g macaddress $macaddress
echo "Your new MAC address is $macaddress"
sudo networksetup -setairportpower en0 off
sudo networksetup -setairportpower en0 on
end