Created
April 5, 2010 19:59
-
-
Save tvwerkhoven/356784 to your computer and use it in GitHub Desktop.
Generate a random MAC address
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
#!/bin/bash | |
# | |
# Generate a random MAC address with a known vendor prefix from the nmap mac | |
# prefixes file. | |
# | |
# Tim van Werkoven, 20100405 <[email protected]> | |
# 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