Skip to content

Instantly share code, notes, and snippets.

@ThinGuy
Last active March 16, 2018 13:40
Show Gist options
  • Save ThinGuy/870a55490b1c0394a5ee81b9bacaf0e2 to your computer and use it in GitHub Desktop.
Save ThinGuy/870a55490b1c0394a5ee81b9bacaf0e2 to your computer and use it in GitHub Desktop.
Function to create a range of IPv6 addresses based off a randomly generated MAC address
random-ipv6-range() {
local DESC="\e[1m${FUNCNAME}: Create a sequential range of IP addresses\e[0m\n"
[[ $1 = '--desc' ]] && { printf "${DESC}";return; }
random-ipv6-range_usage() {
printf "\n${DESC}\n"
printf "\e[2GUsage: ${FUNCNAME%%_*} [OPTIONS]\n\n"
printf "\e[2GOptions:\n"
printf "\e[4G -c, --count \e[24GNumber of IPv6 addresses to include in the range\n"
printf "\e[4G -q, --quiet \e[24GJust print start and end numbers\n"
printf "\n\e[2GEx:\n"
printf "\e[4G \"${FUNCNAME%%_*}\" will output:\n"
printf "\e[4G Creating a range of 100 seqential IPv6 Addresses...\n\n"
printf "\e[4G \e[1mStart End\e[0m\n"
printf "\e[4G fe80::5454:f6ff:fee2:9494 fe80::5454:f6ff:fee2:94f7\n\n"
printf "\e[2GEx:\n"
printf "\e[4G \"${FUNCNAME%%_*} -c 400 -q\" will output:\n"
printf "\e[4G fe80::5454:dfff:feae:1697 fe80::5454:dfff:feae:16fa\n\n"
printf "\n\e[2GNotes:\n"
printf "\e[4G - The Starting IPv6 Address is created using\n"
printf "\e[7GStateless Address Autoconfiguration (SLAAC) principles,\n"
printf "\e[7Gwith the local-link address being created from a randomly \n"
printf "\e[7Ggenerated virtual MAC Address\n\n"
printf "\e[4G - The starting address is then incremented n times \n"
printf "\e[7Gwhere n =the value entered with the -c,--count option\n\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"
}
local QUIET=false
ARGS=`getopt -o c:qh -l count:,quiet,help -n ${FUNCNAME} -- "$@"`
eval set -- "$ARGS"
while true ; do
case "$1" in
-c|--count) local COUNT="${2}";shift 2;;
-q|--quiet) local QUIET=true;shift 1;;
-h|--help) ${FUNCNAME}_usage;return 0;;
--) shift;break;;
esac
done
[[ -n ${COUNT} && $((10#${COUNT})) =~ ^[0-9]+$ ]] || { [[ -n ${COUNT} ]] && printf "\e[1m${COUNT} is invalid.\e[0m\n\e[2GUsing default of 100\n\n";local COUNT=100; }
[[ -z ${COUNT} ]] && local COUNT=100
[[ ${QUIET} = false ]] && printf "Creating a range of ${COUNT} seqential IPv6 Addresses...\n\n"
local START=$(printf "%d\n" 0x"$(printf '5254%02x%02x%02x%02x\n' $((RANDOM%256)) $((RANDOM%256)) $((RANDOM%256)) $((RANDOM%256)))")
local -a RAND_RANGE=($(
for MAC in $(printf "%x\n" $(seq ${START} 1 $(((${START}-1)+${COUNT})))|sed 's/..\B/&:/g');do
local -a OCTETS=( ${MAC//:/ } ); OCTETS[0]=$(printf %02x $((16#${OCTETS[0]}+0x02)));
printf "fe80::${OCTETS[0]}${OCTETS[1]}:${OCTETS[2]}ff:fe${OCTETS[3]}:${OCTETS[4]}${OCTETS[5]}\n";
done
))
[[ ${QUIET} = false ]] && (printf "Start|End\n${RAND_RANGE[0]}|${RAND_RANGE[-1]}\n")|column -nexts"|"|sed '1s/.*$/'$(printf "\e[1m&\e[0m")'/'
[[ ${QUIET} = true ]] && printf "${RAND_RANGE[0]}|${RAND_RANGE[-1]}\n"|column -nexts"|"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment