Skip to content

Instantly share code, notes, and snippets.

@vitalk
Created July 29, 2014 06:51
Show Gist options
  • Save vitalk/0d668cf67f81326c6a87 to your computer and use it in GitHub Desktop.
Save vitalk/0d668cf67f81326c6a87 to your computer and use it in GitHub Desktop.
Returns the unique and valid MAC address in format 00:16:3E:xx:xx:xx.
#!/usr/bin/env bash
#
#: usage: mac <string>
#:
#: Returns the unique and valid MAC address in format 00:16:3E:xx:xx:xx.
#: The <string> uses to create an unique part of the address.
function help() {
sed -En 's/^#:[[:space:]]?//p' $0
}
function hex() {
local unique=$1 || $(hostname -f 2>/dev/null)
echo 00163e$(echo $unique | md5)
}
function mac() {
echo $(hex $1) | sed -e 's/\(..\)\(..\)\(..\)\(..\)\(..\)\(..\).*/\1:\2:\3:\4:\5:\6/'
}
if [[ $# -ne 1 ]]; then
help && exit;
fi
mac "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment