Created
August 18, 2015 18:53
-
-
Save mej/592da948323fc58455c1 to your computer and use it in GitHub Desktop.
[BASH] Function to convert MAC address to IPv6 link-local 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
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