Random MAC address generation with jot and ksh

Here's a snippet I use to randomize my if's MAC-48 address. The script makes use of a seed, $1. Note that using a new random address too frequently might just fill up a DHCP server's lease table unnecessarily. Note that this is for a client machine, like a laptop. Depending on the use, modify to clear the two bits referred to in the comment, or not:


# $1 is seed

# Generate, but clear MSBytes's 2 LSB's to specify 'globally unique' and 'unicast'.
printf "%02x:" $(($(jot -r 1 0 255 $(($1+1))) & 2#11111100))
jot -r -s : -w %02x 5 0 255 $1

Then, on FreeBSD I can change $if's MAC-48 (driver permitting, in my case iwn) as follows:

ifconfig $if link $random_mac

If you use DHCP to configure $if it is a good idea to clean out the lease file to avoid requesting the same IP for a different MAC address, leading to problems. On FreeBSD, the lease file for this interface would be /var/db/dhclient.leases.$if.