-
-
Save mbierman/03b2a962ac04963ef5bbc8354d0ed5d1 to your computer and use it in GitHub Desktop.
#!/bin/bash | |
# v 1.0.2 | |
# https://gist.github.com/mbierman/03b2a962ac04963ef5bbc8354d0ed5d1 | |
# 2022 mbierman | |
sleep="${1:-5}" | |
regex="^([0-9A-F]{2}[:]){5}([0-9A-F]{2})$" | |
getmac () { | |
rnd=$(openssl rand -hex 6 | sed 's/\(..\)\(..\)\(..\)\(..\)\(..\)\(..\)/\1:\2:\3:\4:\5:\6/') | |
echo -e "What is the mac address want to set the mac\n\nexample: CA:49:63:9F:78:27\n\nLeave blank to get one generated for you." | |
read tmp | |
mac1=${tmp:=$rnd} | |
mac=$(echo $mac1 | tr '[:lower:]' '[:upper:]') | |
if [[ $mac =~ $regex ]] ; then | |
comp=true | |
echo -e "\n\n Using $mac...\n" | |
else | |
comp=false | |
echo false | |
fi | |
} | |
until [ "$comp" = "true" ] | |
do | |
getmac | |
done | |
getAdapter () { | |
echo "What is the adapter you want to set the mac for [en0,en1, etc. use 'ifconfig' if you don't know]" | |
read adapter | |
} | |
until [ "$adapter" ] | |
do | |
getAdapter | |
done | |
type=$(ifconfig $adapter | grep baseT) | |
if [ "$type" ]; then | |
echo "got Ethernet" | |
type="Ethernet" | |
else | |
echo "got Wi-Fi" | |
type="Wi-Fi" | |
fi | |
read -p "Is $adapter $type (y|n) ? " -n 1 -r | |
if [[ ! $REPLY =~ ^[Yy]$ ]]; then | |
echo "Exiting. Run again" | |
exit | |
fi | |
sudo ifconfig $adapter down && sleep $sleep && sudo ifconfig $adapter up && \ | |
sudo ifconfig $adapter ether $mac && sudo networksetup -detectnewhardware |
also I think grep baseT
is not correct as all adapters are wifi in this way
@phush0 "baseT" should never be on Wi-Fi. It should be an indicator of Ethernet.
Try the same code but add this:
randommac.sh 1
Note Ethernet
bigmac:bin michael$ ifconfig en0
en0: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
media: autoselect (1000baseT <full-duplex>)
status: active
Note Wi-FI
bigmac:bin michael$ ifconfig en1
en1: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
options=6463<RXCSUM,TXCSUM,TSO4,TSO6,CHANNEL_IO,PARTIAL_CSUM,ZEROINVERT_CSUM>
ether
inet6 prefixlen 64 secured scopeid 0xc
inet 192.168.0.41 netmask 0xffffff00 broadcast 192.168.0.255
nd6 options=201<PERFORMNUD,DAD>
media: autoselect
status: active
nope, same error
@phush0 so I think this is a race issue. I think it will depend on how fast your machine tries to assign an IP to your adapter. Try incrementing until you find a number that works.
randommac.sh 3 if that doesn't work
randommac.sh 4...
I bet it will be consistent for your machine. ">=4" worked for me.
+ regex='^([0-9A-F]{2}[:]){5}([0-9A-F]{2})$'
+ '[' '' = true ']'
+ getmac
++ openssl rand -hex 6
++ sed 's/\(..\)\(..\)\(..\)\(..\)\(..\)\(..\)/\1:\2:\3:\4:\5:\6/'
+ rnd=83:6b:07:c1:b9:3f
+ echo -e 'What is the mac address want to set the mac\n\nexample: 4C:77:CB:9A:18:89\n\nLeave blank to get one generated for you.'
What is the mac address want to set the mac
example: 4C:77:CB:9A:18:89
Leave blank to get one generated for you.
+ read tmp
4C:77:CB:9A:18:89
+ mac1=4C:77:CB:9A:18:89
++ echo 4C:77:CB:9A:18:89
++ tr '[:lower:]' '[:upper:]'
+ mac=4C:77:CB:9A:18:89
+ [[ 4C:77:CB:9A:18:89 =~ ^([0-9A-F]{2}[:]){5}([0-9A-F]{2})$ ]]
+ comp=true
+ echo -e '\n\n Using 4C:77:CB:9A:18:89...\n'
Using 4C:77:CB:9A:18:89...
+ '[' true = true ']'
+ '[' '' ']'
+ getAdapter
+ echo 'What is the adapter you want to set the mac for [en0,en1, etc. use '\''ifconfig'\'' if you don'\''t know]'
What is the adapter you want to set the mac for [en0,en1, etc. use 'ifconfig' if you don't know]
+ read adapter
en0
+ '[' en0 ']'
++ ifconfig en0
++ grep baseT
+ type=
+ '[' '' ']'
+ echo 'got Wi-Fi'
got Wi-Fi
+ type=Wi-Fi
+ read -p 'Is en0 Wi-Fi (y|n) ? ' -n 1 -r
Is en0 Wi-Fi (y|n) ? y+ [[ ! y =~ ^[Yy]$ ]]
+ sudo ifconfig en0 down
Password:
+ sleep 2
+ sudo ifconfig en0 up
+ sudo ifconfig en0 ether 4C:77:CB:9A:18:89
ifconfig: ioctl (SIOCAIFADDR): Can't assign requested address
ifconfig
lo0: flags=8049<UP,LOOPBACK,RUNNING,MULTICAST> mtu 16384
options=1203<RXCSUM,TXCSUM,TXSTATUS,SW_TIMESTAMP>
inet 127.0.0.1 netmask 0xff000000
inet6 ::1 prefixlen 128
inet6 fe80::1%lo0 prefixlen 64 scopeid 0x1
nd6 options=201<PERFORMNUD,DAD>
gif0: flags=8010<POINTOPOINT,MULTICAST> mtu 1280
stf0: flags=0<> mtu 1280
anpi2: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
options=400<CHANNEL_IO>
ether 7a:12:6c:77:28:17
inet6 fe80::7812:6cff:fe77:2817%anpi2 prefixlen 64 scopeid 0x4
nd6 options=201<PERFORMNUD,DAD>
media: none
status: inactive
anpi1: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
options=400<CHANNEL_IO>
ether 7a:12:6c:77:28:16
inet6 fe80::7812:6cff:fe77:2816%anpi1 prefixlen 64 scopeid 0x5
nd6 options=201<PERFORMNUD,DAD>
media: none
status: inactive
anpi0: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
options=400<CHANNEL_IO>
ether 7a:12:6c:77:28:15
inet6 fe80::7812:6cff:fe77:2815%anpi0 prefixlen 64 scopeid 0x6
nd6 options=201<PERFORMNUD,DAD>
media: none
status: inactive
en4: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
options=400<CHANNEL_IO>
ether 7a:12:6c:77:28:f5
nd6 options=201<PERFORMNUD,DAD>
media: none
status: inactive
en5: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
options=400<CHANNEL_IO>
ether 7a:12:6c:77:28:f6
nd6 options=201<PERFORMNUD,DAD>
media: none
status: inactive
en6: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
options=400<CHANNEL_IO>
ether 7a:12:6c:77:28:f7
nd6 options=201<PERFORMNUD,DAD>
media: none
status: inactive
en1: flags=8963<UP,BROADCAST,SMART,RUNNING,PROMISC,SIMPLEX,MULTICAST> mtu 1500
options=460<TSO4,TSO6,CHANNEL_IO>
ether 36:20:31:12:2c:c0
media: autoselect <full-duplex>
status: inactive
en2: flags=8963<UP,BROADCAST,SMART,RUNNING,PROMISC,SIMPLEX,MULTICAST> mtu 1500
options=460<TSO4,TSO6,CHANNEL_IO>
ether 36:20:31:12:2c:c4
media: autoselect <full-duplex>
status: inactive
en3: flags=8963<UP,BROADCAST,SMART,RUNNING,PROMISC,SIMPLEX,MULTICAST> mtu 1500
options=460<TSO4,TSO6,CHANNEL_IO>
ether 36:20:31:12:2c:c8
media: autoselect <full-duplex>
status: inactive
ap1: flags=8802<BROADCAST,SIMPLEX,MULTICAST> mtu 1500
options=400<CHANNEL_IO>
ether be:d0:74:4a:0e:68
media: autoselect
status: inactive
en0: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
options=6463<RXCSUM,TXCSUM,TSO4,TSO6,CHANNEL_IO,PARTIAL_CSUM,ZEROINVERT_CSUM>
ether ca:49:63:9f:78:27
inet6 fe80::14d9:a7eb:85d7:1434%en0 prefixlen 64 secured scopeid 0xe
inet 192.168.1.103 netmask 0xffffff00 broadcast 192.168.1.255
nd6 options=201<PERFORMNUD,DAD>
media: autoselect
status: active
bridge0: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
options=63<RXCSUM,TXCSUM,TSO4,TSO6>
ether 36:20:31:12:2c:c0
Configuration:
id 0:0:0:0:0:0 priority 0 hellotime 0 fwddelay 0
maxage 0 holdcnt 0 proto stp maxaddr 100 timeout 1200
root id 0:0:0:0:0:0 priority 0 ifcost 0 port 0
ipfilter disabled flags 0x0
member: en1 flags=3<LEARNING,DISCOVER>
ifmaxaddr 0 port 10 priority 0 path cost 0
member: en2 flags=3<LEARNING,DISCOVER>
ifmaxaddr 0 port 11 priority 0 path cost 0
member: en3 flags=3<LEARNING,DISCOVER>
ifmaxaddr 0 port 12 priority 0 path cost 0
nd6 options=201<PERFORMNUD,DAD>
media: <unknown type>
status: inactive
awdl0: flags=8943<UP,BROADCAST,RUNNING,PROMISC,SIMPLEX,MULTICAST> mtu 1500
options=400<CHANNEL_IO>
ether de:84:39:6d:57:b5
inet6 fe80::dc84:39ff:fe6d:57b5%awdl0 prefixlen 64 scopeid 0x10
nd6 options=201<PERFORMNUD,DAD>
media: autoselect
status: active
llw0: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
options=400<CHANNEL_IO>
ether de:84:39:6d:57:b5
inet6 fe80::dc84:39ff:fe6d:57b5%llw0 prefixlen 64 scopeid 0x11
nd6 options=201<PERFORMNUD,DAD>
media: autoselect
status: active
utun0: flags=8051<UP,POINTOPOINT,RUNNING,MULTICAST> mtu 1380
inet6 fe80::538e:e939:a9fe:f117%utun0 prefixlen 64 scopeid 0x12
nd6 options=201<PERFORMNUD,DAD>
utun1: flags=8051<UP,POINTOPOINT,RUNNING,MULTICAST> mtu 2000
inet6 fe80::d77d:71a7:334:f09d%utun1 prefixlen 64 scopeid 0x13
nd6 options=201<PERFORMNUD,DAD>
utun2: flags=8051<UP,POINTOPOINT,RUNNING,MULTICAST> mtu 1000
inet6 fe80::ce81:b1c:bd2c:69e%utun2 prefixlen 64 scopeid 0x14
nd6 options=201<PERFORMNUD,DAD>
utun3: flags=8051<UP,POINTOPOINT,RUNNING,MULTICAST> mtu 1380
inet6 fe80::78f8:4e9f:d0f3:1fb3%utun3 prefixlen 64 scopeid 0x15
nd6 options=201<PERFORMNUD,DAD>
utun4: flags=8051<UP,POINTOPOINT,RUNNING,MULTICAST> mtu 1380
inet6 fe80::6f5e:6fba:9e7d:b881%utun4 prefixlen 64 scopeid 0x16
nd6 options=201<PERFORMNUD,DAD>
well script is working only with original adapter from Apple. If I try to change mac on the dock adapter it is not working. So it sounds like Apple fault.
@phush0 which one is failing? en8
?
@FASHION399 please try the new version.
yep en8, I tried and Dell one on en9 with same fail result
still trying to get it, what's the difference between this simple action that worked couple days ago for me:
- disconnect from wifi network (do not turn off wifi adapter)
openssl rand -hex 6 | sed 's/\(..\)/\1:/g; s/.$//'
sudo ifconfig en0 ether xx:xx:xx:xx:xx:xx
- connect back to local wlan
and this script???? o_0
the main difference I see is in openssl generator (syntax is a little bit different)
This is the only script that worked for me on MacStudio Wifi / Sonoma. I will try on Macbook Pro.
Thanks a lot and please keep it alive.
Thanks this is working great for en0 wi-fi!
This script doesn't work for some people because when it runs "sudo ifconfig $adapter up", it auto-associates with a known SSID if a known SSID is found. The wifi adapter needs to be on and not associated for it to work. That's why the following step works, "disconnect from wifi network (do not turn off wifi adapter)".
It's not woking on MACOS sonoma 14.4.1 (23E224), MacBook Air, Apple M1.
It showed the error message: "ifconfig: ioctl (SIOCAIFADDR): Can't assign requested address"
This script doesn't work for some people because when it runs "sudo ifconfig $adapter up", it auto-associates with a known SSID if a known SSID is found. The wifi adapter needs to be on and not associated for it to work. That's why the following step works, "disconnect from wifi network (do not turn off wifi adapter)".
I don’t quite follow. Can you explain what you mean?
It's not woking on MACOS sonoma 14.4.1 (23E224), MacBook Air, Apple M1. It showed the error message: "ifconfig: ioctl (SIOCAIFADDR): Can't assign requested address"
What is the interface of your Wi-Fi adapter?
Looks like Apple killed this in Sonoma. There may be no practical workaround. If I find one, I'll update.
still trying to get it, what's the difference between this simple action that worked couple days ago for me:
- disconnect from wifi network (do not turn off wifi adapter)
openssl rand -hex 6 | sed 's/\(..\)/\1:/g; s/.$//'
sudo ifconfig en0 ether xx:xx:xx:xx:xx:xx
- connect back to local wlan
and this script???? o_0
- disconnect from wifi network (do not turn off wifi adapter)
This is very useful for me!
no change