Skip to content

Instantly share code, notes, and snippets.

@wowstrongdrink
Created February 4, 2011 17:48
Show Gist options
  • Save wowstrongdrink/811444 to your computer and use it in GitHub Desktop.
Save wowstrongdrink/811444 to your computer and use it in GitHub Desktop.
Set a random mac address on Snow Leopard (only until next reboot)
#!/usr/bin/env ruby
#
# Script to change to a random mac address
# Usage: ruby random_mac_address
#
# Modeled after instructions at:
# http://www.iclarified.com/entry/index.php?enid=7673
#
# Author:: Marc Siegel <wowstrongdrink@yahoo.com>
# Date:: 2011-02-11
#
def cur_mac_addr
`ifconfig en1 | grep ether`.gsub('ether', '').strip
end
def set_mac_addr(new_addr)
`sudo ifconfig -v en1 lladdr #{new_addr}`
`sudo ifconfig -v en1 ether #{new_addr}`
end
def generate_mac_addr
random_48bit_int = rand(2**48)
padded_hex = "%012x" % random_48bit_int
digit_pairs = padded_hex.split('').inject([]) do |arr, d|
if arr.last && arr.last.size == 1
arr.last << d
else
arr << d
end
arr
end
new_addr = digit_pairs.join(':')
end
def disconnect_from_wireless_networks
`sudo /System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport -z`
end
##
# Main logic here
##
puts "\n(Disconnecting from any wireless network...)"
disconnect_from_wireless_networks
old_addr = cur_mac_addr
puts "\nOld mac address:\t #{old_addr}"
new_addr = generate_mac_addr
puts "Generated new address:\t #{new_addr}\n\n"
(1..10).each do |attempt|
puts "Attempting to set new address (try ##{attempt})..."
set_mac_addr(new_addr)
break if cur_mac_addr == new_addr
sleep 1
end
puts ""
if cur_mac_addr == new_addr
puts "New mac address: #{new_addr}\n\n"
else
puts "Unable to set new mac address. "
puts "Try turning airport off and then back on, and running again."
end
puts ""
@wowstrongdrink
Copy link
Author

Ok, updated to try up to 10 times, to use lladdr as well as ether, and to advice power-cycling airport after failure, which does seem to work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment