Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Generate a random MAC address
#!/bin/bash
#
# Generate a random MAC address with a known vendor prefix from the nmap mac
# prefixes file.
#
# Tim van Werkoven, 20100405 <t.i.m.vanwerkhoven@xs4all.nl>
# This file is licensed under the Creative Commons Attribution-Share Alike
# license versions 3.0 or higher, see
# http://creativecommons.org/licenses/by-sa/3.0/
## Get a random prefix from a nmap-mac-prefixes file
MACFILE="/sw/share/nmap/nmap-mac-prefixes"
LINES=`wc -l $MACFILE | awk '{print$1}'`;
## Select a random line from the file
RANDLINE=`expr $RANDOM % $LINES`
## Line syntax is <prefix> <vendor>, with <prefix> a 6 digit hexadecimal
## number. Add colons and convert to lowercase if necessary.
VENDOR=`head -n $RANDLINE $MACFILE | tail -n 1 | awk '{print$2}'`
PREFIX=`head -n $RANDLINE $MACFILE | tail -n 1 | awk '{print$1}' | sed -n 's/\(..\)\(..\)\(..\)/\1:\2:\3/p' | tr [:upper:] [:lower:]`
## Using the fixed prefix, fill in the remaining 6 digits with random numbers
MACADDR=$PREFIX
i=0;
while [ $i -lt 3 ]; do
printf -v MACADDR "%s:%02x" $MACADDR `expr $RANDOM % 255`
let i+=1
done
printf "Vendor: %s, vendor prefix: %s, MAC: %s\n" $VENDOR $PREFIX $MACADDR
printf "sudo ifconfig en1 lladdr $PREFIX\n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.