I just thought I had need to blog about that, a script to generate a random MAC address. It is based on $RANDOM from any shell and works reliably.
#! /bin/sh
# Generates a random mac address
echo $RANDOM | openssl md5 | sed 's/\(..\)/\1:/g' | cut -b0-17
Cheers!
very nice 1 liner
ReplyDeleteMaster Foo would be pleased
# echo $RANDOM | openssl md5 | sed 's/\(..\)/\1:/g' | cut -b0-17
ReplyDeletecut: [-cf] list: values may not include zero
# echo $RANDOM | openssl md5
(stdin)= 4b4f98e306dd3797ca1a05c8349b8005
Gives (stdin) at the begining...but:
# echo $RANDOM | md5 | sed 's/\(..\)/\1:/g' | cut -c1-17
c6:af:4a:15:08:4b
...gives the result. This is on NetBSD though. Thnx for the tip, nice oneliner. :)
Reading the comments... and reading the blog entry, I was also wondering for the cut command. This has changed since some time in the GNU coreutils, 0 prints an error now.
ReplyDeleteAs on my router ther is no md5 command,
ReplyDeleteI did:
RANDOM=$(md5sum /proc/uptime | sed 's/\(..\)/\1:/g' | cut -c1-17)
regards, wwwolf
No need for cut anyway...
ReplyDeletesed -r 's/(..)/\1:/g; s/^(.{14}).*$/\1/;'
Also, I found /dev/random or /dev/urandom to be a more appropriate/useful source of random hex:
MAC=00:`head -c5 /dev/urandom|hexdump -e '"%x"'| sed -r 's/(..)/\1:/g; s/:$//;'`
Very nice use of hexdump, however to be sure to always have two hexa values %02x must be used for the format.
ReplyDeletethe '-r' flag for sed cannot be used on MacOSX (10.6), use '-E' instead
ReplyDelete"cho $RANDOM | openssl md5 | sed 's/\(..\)/\1:/g' | cut -b0-17"
ReplyDeleteHey so I am starting to learn how to script and I was looking into mac generators..
I find this crazy line of code that looks like a little dude with his arms in the air
" /\(..\)/\ "
And thru some witch craft this little dude spits out a mac address......
I have no idea how it works but it does..
This is an awesome piece of art Mike, nice job.
" /\(..\)/\1 "
ReplyDelete/ starts the expression
\ escapes the (
( begins a sort of "clipboard"
. finds any character
. finds any character
\ escapes the )
) closes the clipboard
/ ends the search, begins the replacement
\ espaces the 1
1 pastes the clipboard