Skip to content

Instantly share code, notes, and snippets.

@chicken-suop
Last active May 12, 2022 20:09
Show Gist options
  • Save chicken-suop/dc134dcecc3716a5c42e28cc3fbd6d3b to your computer and use it in GitHub Desktop.
Save chicken-suop/dc134dcecc3716a5c42e28cc3fbd6d3b to your computer and use it in GitHub Desktop.
Disassociate, generate new mac, reconnect
#!/bin/bash
# arp -ani en0
# ^^ Get MAC addrs of other users on same network.
# Change en0 to your network interface.
# On macos, this can be seen through:
# networksetup -listallhardwareports
# sudo ifconfig en0 ether $MAC
# ^^ replace $MAC with an addr from above command
deviceOverride="${1}"
if [ -n $deviceOverride ]; then
ifout="$(ifconfig $deviceOverride 2>/dev/null)"
echo "$ifout" | grep 'status: active' > /dev/null 2>&1
rc="$?"
if [ "$rc" -eq 0 ]; then
currentservice="User input"
currentdevice="$deviceOverride"
currentmac=$(echo "$ifout" | awk '/ether/{print $2}')
fi
else
services=$(networksetup -listnetworkserviceorder | grep 'Hardware Port')
while read line; do
sname=$(echo $line | awk -F "(, )|(: )|[)]" '{print $2}')
sdevice=$(echo $line | awk -F "(, )|(: )|[)]" '{print $4}')
#echo "Current service: $sname, $sdevice, $currentservice"
if [ -n "$sdevice" ]; then
ifout="$(ifconfig $sdevice 2>/dev/null)"
echo "$ifout" | grep 'status: active' > /dev/null 2>&1
rc="$?"
if [ "$rc" -eq 0 ]; then
currentservice="$sname"
currentdevice="$sdevice"
currentmac=$(echo "$ifout" | awk '/ether/{print $2}')
fi
fi
done <<< "$(echo "$services")"
fi
if [ -n "$currentservice" ]; then
printf "Service: $currentservice\nDevice: $currentdevice\nMAC:$currentmac\n"
echo "Getting network password"
ssid=$(/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport -I | awk '/ SSID/ {print substr($0, index($0, $2))}')
netpass=$(security find-generic-password -ga $ssid | awk -F'"' '/password:/{print $2}')
echo "Disassociating from current network"
sudo /System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport -z
echo "Generating new MAC addr and assigning it"
sudo ifconfig $currentdevice ether $(openssl rand -hex 6 | sed 's/\(..\)/\1:/g; s/./0/2; s/.$//')
echo "Detecting new network hardware"
networksetup -detectnewhardware
newmac=$(ifconfig $currentdevice | awk '/ether/{print $2}')
printf "New MAC: $newmac\n"
echo "Reconnecting to the network"
networksetup -setairportnetwork $currentdevice $ssid $netpass
echo "Opening captive portal network"
# This should open in default browser, but that wasn't working for me
# open http://captive.apple.com/hotspot-detect.html
# So use this to open in your favourite app
# open -a "App Name" http://captive.apple.com/hotspot-detect.html
open -a "Google Chrome Canary" http://captive.apple.com/hotspot-detect.html
else
>&2 echo "Could not find current service"
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment