Hi, isn't Python now shipping with OSX? I haven't tested it, but wouldn't this also work while being more readable?
(DISCLAIMER: I haven't tested it. Also, I haven't tested it.)
from random import randint
from os import getenv
from subprocess import call
from sys import argv
if len(argv) > 1:
# get network interface from cmdline parameter
intf = argv[1]
else:
# get network interface from ENVIRONMENT variables
intf = getenv('INTERFACE', '')
if intf:
# generate random mac address
r = lambda: randint(0,255)
randmac = '%02X:%02X:%02X:%02X:%02X:%02X' % (r(),r(),r(),r(),r(),r())
# change mac address
call(["ifconfig", "%s hw ether %s" % (intf, randmac)])
else:
print "Couldn't determine network interface"
This code will lead to a nice, self-inflicted bug that will sometimes break your network connection. The low two bits of the first byte of the MAC have a special purpose and should be kept at 0 [1]. Learned this the hard way when developing on an embedded TCP stack. The switch silently discarded packets from multicast or local MAC addresses.
It's not like this is obfuscated code. I clearly mentioned this is untested, and it turns out it contains a major bug. But it starts a conversation, and made me learn about that significant bit in the mac address, should I pay a fine for starting a conversation?
Hi again, here is a version that corrects the bug addressed by lmb:
from random import randint
from random import choice
from os import getenv
from subprocess import call
from sys import argv
if len(argv) > 1:
# get network interface from cmdline parameter
intf = argv[1]
else:
# get network interface from ENVIRONMENT variables
intf = getenv('INTERFACE', '')
if intf:
# generate random mac address
r = lambda: randint(0,255)
# Universally or Locally Administered Bit and Individual/Group Bit
# See http://packetsdropped.wordpress.com/2011/01/13/
# mac-address-universally-or-locally-administered-bit-and-individualgroup-bit/
sbyte = choice(['%02X' % i for i in range(255) \
if bin(i)[-2] == '0' and \
bin(i)[-1] == '0'])
randmac = sbyte + ':%02X:%02X:%02X:%02X:%02X' % (r(),r(),r(),r(),r())
# change mac address
call(["ifconfig", "%s hw ether %s" % (intf, randmac)])
else:
print "Couldn't determine network interface"
I'll try this again, but on a recent clean install of Ubuntu 14.04 macchanger didn't appear to do anything (it worked perfectly on 12.04). I was interested in the option to randomly spoof a different one at reboot but didn't get it working after several tries.
Just was going to comment that is someone wants to track you they'd probably track your mobile device which is much harder to customize. Looks like somebody at Apple had the same thought.
Honestly using MacPorts is a terrible idea. If you need non-standard packages uses homebrew -- which goes out of its way not to break system components, unlike MacPorts.
Huh? The whole point of MacPorts is that it builds its own dependencies, thereby not touching the "system components" at all. It's entirely self-contained in /opt/local, or /Applications/MacPorts for GUI apps. I've been using it for many years and it's never broken anything. (Except occasionally itself.)
I would argue that you're far more likely to have stuff already installed in /usr/local than in /opt/local.
Homebrew is currently trendy but there was nothing wrong with MacPorts. Declaring the entire project to be "a terrible idea" is simply ignorant.
I've been using MacPorts for the better part of decade and some ports in it clobber system files without warning. I've been bitten one too many times by that
The file that you download is oui.txt. The shell script visible on that page is what you're supposed to save as macrandomize.sh. My guess is that he leaves a few steps out so that only people who know what they're doing actually attempt this.
You want unpredictable random numbers, so using bash $RANDOM is no good. hexdump + /dev/urandom + sed would work, incantation left as an exercise to reader as I don't have a Mac around to check limitations of their hexdump(1)...
(Same goes for the Python solution posted in another comment here).
Does this even work on the wireless interface on a mbp? The driver used to prevent you from setting the MAC addr to anything you wanted, vs the ethernet driver for the wired interface that lets you play with it freely.
In my experience, my macbook pro MAC address can be changed with the bash command listed in the file for wifi (sudo ifconfig en1 ether [addr]), but it will not let me change it to any arbitrary address I want, just some of them, including addresses that start with aa:[rest], ab:[rest], etc., which is what I usually go for. Just tested it and en0 doesn't seem to have the same limitations.
I wrote my own little terminal commands back when I had a 5 hour lay-over in Charles de Gaulle Airport (Paris). At the time, they offered 15 minute free wifi, so I just ran my little tool and it worked a treat.
Maybe first ask yourself why have a MAC address at all, then the answer will come to you.
I find it interesting how many people will stumble over this concept. A lot of technically minded people know that a MAC address is a "unique identifier" for the network card. They have that phrase "unique identifier" fixed in their heads and they know that MACs are this. Pull out a question like "why would you want a unique identifier?" and you get a lot of blank looks. It's almost like it's too easy to latch onto a phrase like "unique identifier" and get distracted from its practical purpose. (That thing that tells you whose packet this is.)
^ This. A MAC address is a link-layer network address, just like an IP address. For example, the ARP protocol[1] uses it to identify devices with a given IP address on a network.
I don't know if this was a concern, but some restaurants set time limits on internet access during certain hours based off of mac addresses. You need a different mac addy to bypass the restriction.
Be careful. Randomizing your MAC address may make your computer stick out more than if you pick a single legitimate looking MAC address and stick with it for a while. Not all 16 million MAC prefixes have been sold to manufacturers yet.
One of my friends also told me Dropbox uses MAC address to identify computers. He wrote a similar script to get lots of referrals and free space on Dropbox. Probably against their Terms though.
Tangential, but it's actually heartwarming to me that so far every commenter seems to understand that the "MAC" in the title refers to Media Access Control, rather than a comically incorrect way of spelling the name of the computers that OS X runs on.
https://github.com/feross/SpoofMAC
> spoof-mac randomize en0