In my script I need to randomly generate MAC addresses. The code below is from a larger script extracted, that's why the MAC address calculation is in a separate function.
This works fine with the code below. Although when I execute the script it slows heavily down after a number of generated addresses.
How can I improve the speed of generating valid MAC addresses?
#!/bin/bash
devicesCSVMacAddress="55:2d:fa:07" # <- fake MAC address prefix
devicesCSVFile=''
function mac_address() {
line=''
# ****************
# This line below when I calculate a random mac address ending seems to be slow
line+=$devicesCSVMacAddress$(od -txC -An -N2 /dev/random|tr \ :)
# ****************
devicesCSVFile+=$line'\n'
date
}
for (( i=0; i<100; i++ ))
do
mac_address
echo $i
done
echo -e $devicesCSVFile > devices.csv
I used the od tool like it described in this answer: How to generate a valid random MAC Address with bash shell.
/dev/random
slows because you exhaust the available entropy; see the manpage for random(4). MACs are public on the network so making them cryptorandom is a waste of time; use/dev/urandom
or even bash$RANDOM
. In fact they don't need to be random at all, if you have a convenient way of making them unique. – dave_thompson_085 Nov 15 '16 at 10:36/dev/urandom
does the trick and is sufficient in my case. Thanks all for their contributions! – Bruno Bieri Nov 16 '16 at 08:39