Mac – Spoofing MAC address in a persistent fashion

mac address

So I like to set a new, randomly-generated MAC address after every restart using

ruby -e 'print ("%02x"%((rand 64)*4|2))+(0..4).inject(""){|s,x|s+":%02x"%(rand 256)} + "\n"'

and then copy/pasting the output after the command

sudo ifconfig en0 ether

so that I get a new IP address from my ISP each day. I would like to further automate this process so that on startup my computer automatically runs these two commands, but I don't know enough about CLI syntax to make it work. How can I combine these two commands so that ifconfig takes the generated output of the ruby command in a single step? And then, how can I get that to automatically run at startup?

Thank you.

Best Answer

Run EDITOR=nano sudo crontab -e and add a line like this:

@reboot ifconfig en0 ether $(printf \%02x $(($RANDOM\%128*2)))$(head -c4 /dev/random|hexdump -v -e '/1 ":\%02x"')

Commands scheduled by @reboot are run after you restart or turn the computer on but not after you log out and back in. % has to be escaped as \% in crontab.

From a comment at http://osxdaily.com/2010/11/10/random-mac-address-generator/:

You need to be careful that this does not generate multicast mac addresses, as these are technically illegal as source macs. The strict definition of a multicast mac address is one where the least significant bit of the first byte is set to 1. So if the first octet’s LSB is 1 (01, 03,05, a1, etc) you technically have a multicast mac source. See http://en.wikipedia.org/wiki/MAC_address.

Using multicast src macs might not cause immediate connectivity problems, but certainly has implications for switches learning the mac address preventing unicast flooding, and routers allowing arp to resolve.

Cisco, for example, will not allow mac learning if the source is non-unicast, and Cisco routers will not install arp entries for multicast macs to unicast ip addresses.