Skip to main content
Use sed instead of printf for address shortening; updated explanation
Source Link

I must give kudos to @rubo77 above for a pure-shell (unless you count printf) answer above, which I was seeking. However one thing it doesn't do is

This adds a bit of sed to:

(a) elide any leading zeroes in each 16-bit chunk of the output, a convention followed in output chunk; and

(and accepted as inputb) byelide any further all the IPv6-related CLI tools I've used thus farzero chunks at the start of the IID

...as per common conventions for address brevity.

mac_to_eui64() {
 IFS=':'; set $1; unset IFS
 echo "fe80::$(printf %x $((0x$1 ^ 2))$2)$2:$(printf %x 0x$3){3}ff:fe$4:$(printf$5$6" %x|
 0x$5$6)" sed -E 's/:0+/:/g; s/:{3,}/::/; s/:$/:0/'
}

Note that if you wished to repurpose this for other (arbitrary) prefixes, which you might since EUI-64 can be used in any scope (although it's advised not to use it for global addresses for privacy reasons), you'd wantneed a slightly more convoluted sed script. That's because the fe80:: prefix already contains three zero-chunks (elided to address that other notationthe double-conventioncolon) adjacent to the start of eliding onethe IID. Thus even if the IID's first chunk is all-zeroes (butthat is, if the MAC started with 02:00), it would be elided too. The EUI-64 format only leaves one) occurrence of multiple further chunk that could be all-zero chunks-- the last one -- which the script above adds back in as a single zero. Other prefixes might require one to leave a single zero between two colons.

I must give kudos to @rubo77 above for a pure-shell (unless you count printf) answer above, which I was seeking. However one thing it doesn't do is elide leading zeroes in each 16-bit chunk of the output, a convention followed in output (and accepted as input) by all the IPv6-related CLI tools I've used thus far.

mac_to_eui64() {
 IFS=':'; set $1; unset IFS
 echo "fe80::$(printf %x $((0x$1 ^ 2))$2):$(printf %x 0x$3)ff:fe$4:$(printf %x 0x$5$6)"
}

Note that if you wished to repurpose this for other (arbitrary) prefixes, which you might since EUI-64 can be used in any scope (although it's advised not to use it for global addresses for privacy reasons), you'd want to address that other notation-convention of eliding one (but only one) occurrence of multiple all-zero chunks.

I must give kudos to @rubo77 above for a pure-shell (unless you count printf) answer above, which I was seeking.

This adds a bit of sed to:

(a) elide any leading zeroes in each 16-bit chunk; and

(b) elide any further all-zero chunks at the start of the IID

...as per common conventions for address brevity.

mac_to_eui64() {
 IFS=':'; set $1; unset IFS
 echo "fe80::$(printf %x $((0x$1 ^ 2)))$2:${3}ff:fe$4:$5$6" |
  sed -E 's/:0+/:/g; s/:{3,}/::/; s/:$/:0/'
}

Note that if you wished to repurpose this for other (arbitrary) prefixes, which you might since EUI-64 can be used in any scope (although it's advised not to use it for global addresses for privacy reasons), you'd need a slightly more convoluted sed script. That's because the fe80:: prefix already contains three zero-chunks (elided to the double-colon) adjacent to the start of the IID. Thus even if the IID's first chunk is all-zeroes (that is, if the MAC started with 02:00), it would be elided too. The EUI-64 format only leaves one further chunk that could be all-zero -- the last one -- which the script above adds back in as a single zero. Other prefixes might require one to leave a single zero between two colons.

Source Link

I must give kudos to @rubo77 above for a pure-shell (unless you count printf) answer above, which I was seeking. However one thing it doesn't do is elide leading zeroes in each 16-bit chunk of the output, a convention followed in output (and accepted as input) by all the IPv6-related CLI tools I've used thus far.

mac_to_eui64() {
 IFS=':'; set $1; unset IFS
 echo "fe80::$(printf %x $((0x$1 ^ 2))$2):$(printf %x 0x$3)ff:fe$4:$(printf %x 0x$5$6)"
}

Note that if you wished to repurpose this for other (arbitrary) prefixes, which you might since EUI-64 can be used in any scope (although it's advised not to use it for global addresses for privacy reasons), you'd want to address that other notation-convention of eliding one (but only one) occurrence of multiple all-zero chunks.