Last active
December 30, 2015 17:26
-
-
Save takuya/81cdef93ff867e7686e7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'optparse' | |
def current_mac_address_from_ifconfig() | |
ret = `ifconfig en0` | |
match = ret.match(/([a-f0-9]{2}:*){6}/) | |
mac_addr = match[0] | |
end | |
def default_mac_addr | |
if !File.exists?("/usr/local/etc/mac_addr") then | |
mac_addr = current_mac_address_from_ifconfig | |
open("/usr/local/etc/mac_addr", "w"){|f| f.puts mac_addr } | |
end | |
mac_addr = open("/usr/local/etc/mac_addr").read.strip | |
end | |
def random_mac_addr | |
mac_addr = `openssl rand -hex 6 ` | |
mac_addr = mac_addr.scan(/.{1,2}/).join(":") | |
end | |
def change_mac_addr_cmd_str(mac_addr) | |
cmd = [ | |
# "sudo /System/Library/PrivateFrameworks/Apple80211.framework/Resources/airport -z", | |
"sudo ifconfig en0 ether #{mac_addr} ", | |
# "networksetup -detectnewhardware", | |
"networksetup -setairportpower en0 off", | |
"networksetup -setairportpower en0 on", | |
].map(&:strip).reject{|e| e==""} | |
cmd.join(";") | |
end | |
def main () | |
params = ARGV.getopts('ed') | |
mac_addr = nil | |
if params["d"]==false && params["e"]==false then | |
puts current_mac_address_from_ifconfig | |
exit | |
end | |
begin | |
if params["e"] then | |
mac_addr = random_mac_addr | |
end | |
if params["d"] | |
mac_addr = default_mac_addr | |
end | |
cmd = change_mac_addr_cmd_str(mac_addr) | |
`#{cmd}` | |
raise unless mac_addr == current_mac_address_from_ifconfig | |
puts "change mac_addr to #{mac_addr}" | |
rescue => e | |
puts "#{mac_addr} is not valid. retry...." | |
retry | |
end | |
end | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment