Last active
November 17, 2024 02:38
-
-
Save ggrandes/33a27eb00f49b94fcb0dfeb597226bab to your computer and use it in GitHub Desktop.
Generate Random MAC-Address
This file contains hidden or 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 Random MAC-address | |
# | |
# Locally administered: | |
# X2-XX-XX-XX-XX-XX | |
# X6-XX-XX-XX-XX-XX | |
# XA-XX-XX-XX-XX-XX | |
# XE-XX-XX-XX-XX-XX | |
# | |
# Reference: | |
# https://en.wikipedia.org/wiki/MAC_address | |
# | |
# Original Source: | |
# https://gist.github.com/ggrandes/33a27eb00f49b94fcb0dfeb597226bab | |
# | |
randomMac () { | |
local l=$(tr -dc '26ae' < /dev/urandom | dd bs=1 count=1 status=none) | |
local r=$(tr -dc '0-9a-f' < /dev/urandom | dd bs=11 count=1 status=none) | |
echo "${r:0:1}${l}:${r:1:2}:${r:3:2}:${r:5:2}:${r:7:2}:${r:9:2}" | |
} | |
randomMac |
This file contains hidden or 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/sh | |
# | |
# Generate Random MAC-address | |
# | |
# Locally administered: | |
# X2-XX-XX-XX-XX-XX | |
# X6-XX-XX-XX-XX-XX | |
# XA-XX-XX-XX-XX-XX | |
# XE-XX-XX-XX-XX-XX | |
# | |
# Reference: | |
# https://en.wikipedia.org/wiki/MAC_address | |
# | |
# Original Source: | |
# https://gist.github.com/ggrandes/33a27eb00f49b94fcb0dfeb597226bab | |
# | |
case "$1" in | |
awk) | |
awk ' | |
BEGIN { | |
# generate random mac | |
srand(); | |
for (i = 0; i <= 48; i++) { | |
hex[i]=int(rand() * 10 % 2); | |
} | |
hex[6]=1; # local admin | |
hex[7]=0; # unicast | |
# convert to string | |
mac=""; | |
for (i = 0; i < 48; i+=4) { | |
v=0; | |
for (j = 0; j < 4; j++) { | |
if (hex[i+j] == 1) { | |
v += (2**(3-j)); | |
} | |
} | |
if ((i > 0) && (i % 8 == 0)) { | |
mac=sprintf("%s:%x", mac, v); | |
} else { | |
mac=sprintf("%s%x", mac, v); | |
} | |
} | |
print mac; | |
} | |
' | |
;; | |
hexdump) | |
hexdump -n 6 -e '1/1 "%02x:"' < /dev/urandom | sed -E -e 's|:$|\n|g' -e "s|^(.).|\1$(tr -dc '26ae' < /dev/urandom | dd bs=1 count=1 status=none)|g" | |
;; | |
xxd) | |
xxd -l 6 -p /dev/urandom | sed -E -e 's|(..)|\1:|g' -e 's|:$||g' -e "s|^(.).|\1$(tr -dc '26ae' < /dev/urandom | dd bs=1 count=1 status=none)|g" | |
;; | |
*) | |
echo "$0 <awk|hexdump|xxd>" | |
;; | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment