This script randomizes a MAC address–but I cannot renew DHCP afterwards

bashNetwork

#!/bin/bash
NEWMAC=`openssl rand -hex 6 | sed 's/\(..\)/\1:/g; s/.$//'`
ifconfig $1 ether $NEWMAC
ifconfig $1 down
ifconfig $1 up

I pass en0 or en1 as the sole argument to the script and run as sudo. When I do this, the MAC does change, however I cannot restore connectivity until I reboot and the new MAC is lost. Initially the interface displays no IP, but after a few minutes it displays an IP and claims to have DHCP information. However, I believe this is a cached data and not an actual renewal, as I cannot ping, load pages, or otherwise connect to the Internet.

I have appended this snippet with no improvement in results.

ipconfig set $1 BOOTP
ipconfig set $1 DHCP

Best Answer

If you're on MacOSX 10.5 or later, at the beginning of your script, call sudo /System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport -z. Another thing you could do to improve this is to call the script itself with sudo instead of calling sudo multiple times within.