Skip to content

Instantly share code, notes, and snippets.

@veltlion
Forked from mej/mac_to_ipv6.bash
Created January 16, 2023 04:05
Show Gist options
  • Save veltlion/c874c16187ef8b3c0aa64247146e6b84 to your computer and use it in GitHub Desktop.
Save veltlion/c874c16187ef8b3c0aa64247146e6b84 to your computer and use it in GitHub Desktop.
[BASH] Function to convert MAC address to IPv6 link-local address
mac_to_ipv6() {
local HWADDR="$1" IFACE="$2"
local -a OCTETS
local IPV6ADDR
if [[ -z "$IFACE" ]]; then
IFACE=$(ip route | grep ^default | sed 's/^.* dev //')
fi
OCTETS=( ${HWADDR//:/\ } )
OCTETS[0]=$(printf %02x $((16#${OCTETS[0]}+0x02)))
IPV6ADDR="fe80::${OCTETS[0]}${OCTETS[1]}:${OCTETS[2]}ff:fe${OCTETS[3]}:${OCTETS[4]}${OCTETS[5]}%$IFACE"
echo "$IPV6ADDR"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment