Skip to content

Instantly share code, notes, and snippets.

@ThinGuy
Created March 16, 2018 13:44
Show Gist options
  • Save ThinGuy/4a5113315c233a9674036f9f5b9eb4ce to your computer and use it in GitHub Desktop.
Save ThinGuy/4a5113315c233a9674036f9f5b9eb4ce to your computer and use it in GitHub Desktop.
Function to increment increment a provide MAC address (or use autogenerated MAC)
increment-mac() {
local DESC="\e[1m${FUNCNAME}: Prints incrementing MAC address numbers\e[0m\n"
[[ $1 = '--desc' ]] && { printf "${DESC}";return; }
increment-mac_usage() {
printf "\n${DESC}\n"
printf "\e[2GUsage: ${FUNCNAME%%_*} [OPTIONS]\n\n"
printf "\e[2GOptions:\n"
printf "\e[4G -c, --count \e[20GNumber of MAC address to print\n"
printf "\e[4G -m, --mac \e[20GStarting MAC address to use (default: autogenerated MAC)\n"
printf "\n\e[2GEx:\n"
printf "\e[4G \"${FUNCNAME%%_*} -c 5\" will output:\n"
printf "\e[4G 52:54:ec:59:9a:e9\n"
printf "\e[4G 52:54:ec:59:9a:ea\n"
printf "\e[4G 52:54:ec:59:9a:eb\n"
printf "\e[4G 52:54:ec:59:9a:ec\n"
printf "\e[4G 52:54:ec:59:9a:ed\n\n"
printf "\e[2GEx:\n"
printf "\e[4G \"${FUNCNAME%%_*} -c 5 -m 8c:ae:4c:fd:9e:47\" will output:\n"
printf "\e[4G 8c:ae:4c:fd:9e:47\n"
printf "\e[4G 8c:ae:4c:fd:9e:48\n"
printf "\e[4G 8c:ae:4c:fd:9e:49\n"
printf "\e[4G 8c:ae:4c:fd:9e:4a\n"
printf "\e[4G 8c:ae:4c:fd:9e:4b\n\n"
printf "\n\e[2GNotes:\n"
printf "\e[4G - If -c,--count option is not used, or the argument provided\n"
printf "\e[7G is not a whole number, the default count of 100 will be used\n\n"
printf "\e[4G - If -m,--mac option is not used, or the argument provided\n"
printf "\e[7G is not a valid MAC, a randomly generated MAC address will be used\n\n"
}
local MACREGEX='^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$'
ARGS=`getopt -o c:m:h -l count:,mac:,help -n ${FUNCNAME} -- "$@"`
eval set -- "$ARGS"
while true ; do
case "$1" in
-c|--count) local COUNT="${2}";shift 2;;
-m|--mac) local MAC="${2}";shift 2;;
-h|--help) ${FUNCNAME}_usage;return 0;;
--) shift;break;;
esac
done
[[ -n ${COUNT} && $((10#${COUNT})) =~ ^[0-9]+$ ]] || { [[ -n ${COUNT} ]] && printf "\e[1;31m${COUNT} is invalid.\e[0m\nUsing default of 100\n\n";local COUNT=100; }
[[ -z ${COUNT} ]] && local COUNT=100
[[ -n ${MAC} && ${MAC} =~ ${MACREGEX} ]] || { [[ -n ${MAC} ]] && printf "\e[1;31m${MAC} is not a valid MAC Address.\e[0m\nUsing autogenerated MAC\n\n";local MAC=; }
if [[ -z ${MAC} ]];then
local START=$(printf "%d\n" 0x"$(printf '5254%02x%02x%02x%02x\n' $((RANDOM%256)) $((RANDOM%256)) $((RANDOM%256)) $((RANDOM%256)))")
else
local START=$(printf "%d\n" "0x${MAC//:/}")
fi
printf "%x\n" $(seq ${START} 1 $(((${START}-1)+${COUNT})))|sed 's/..\B/&:/g'
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment