Convert MAC address to Link-local address with bash
Clash Royale CLAN TAG#URR8PPP
How can I convert a Mac address into an ipv6 Link-Local address?
you have to add fe80::
at the start and insert ff:fe
in the middle
furthermore all leading zeros must be stripped
bash shell string ipv6
add a comment |
How can I convert a Mac address into an ipv6 Link-Local address?
you have to add fe80::
at the start and insert ff:fe
in the middle
furthermore all leading zeros must be stripped
bash shell string ipv6
1
You also have to flip the universal/local bit.
– Michael Hampton
Jun 14 '13 at 0:08
add a comment |
How can I convert a Mac address into an ipv6 Link-Local address?
you have to add fe80::
at the start and insert ff:fe
in the middle
furthermore all leading zeros must be stripped
bash shell string ipv6
How can I convert a Mac address into an ipv6 Link-Local address?
you have to add fe80::
at the start and insert ff:fe
in the middle
furthermore all leading zeros must be stripped
bash shell string ipv6
bash shell string ipv6
edited Jun 13 '13 at 23:20
Gilles
527k12710561580
527k12710561580
asked Jun 13 '13 at 22:48
rubo77
7,4102469132
7,4102469132
1
You also have to flip the universal/local bit.
– Michael Hampton
Jun 14 '13 at 0:08
add a comment |
1
You also have to flip the universal/local bit.
– Michael Hampton
Jun 14 '13 at 0:08
1
1
You also have to flip the universal/local bit.
– Michael Hampton
Jun 14 '13 at 0:08
You also have to flip the universal/local bit.
– Michael Hampton
Jun 14 '13 at 0:08
add a comment |
3 Answers
3
active
oldest
votes
You can use IFS
to split the MAC address into 6 colon-separated groups and assemble them. You'll also need to flip the 7th most significant bit (thanks bahamat), i.e. bit 1 of the first byte.
mac_to_ipv6 ()
IFS=':'; set $1; unset IFS
ipv6_address="fe80::$(printf %02x $((0x$1 ^ 2)))$2:$3ff:fe$4:$5$6"
You can use the prefix and suffix stripping constructs $VAR#PREFIX
and $VAR%SUFFIX
to cut the MAC address into pieces.
mac_to_ipv6 ()
mac=$1
ipv6_address=fe80::$(printf %02x $((0x$mac%%:* ^ 2)))
mac=$mac#*:
ipv6_address=$ipv6_address$mac%:*:*:*ff:fe
mac=$mac#*:*:
ipv6_address=$ipv6_address$mac%:*$mac##*:
You can use the substring construct (bash only, not sh).
mac_to_ipv6 ()
local mac=$1 byte0
printf %02x -v byte0 $((0x$mac:0:2 ^ 2))
ipv6_address="fe80::$byte0$mac:3:5ff:fe$mac:9:5$mac:15:2"
This doesn't work because link-local addresses have the 7th bit flipped high.
– bahamat
Jun 14 '13 at 5:35
This doesn't seem to work any more, I posted a working version below
– rubo77
Dec 16 at 8:15
add a comment |
Taking Gilles' explanation, but correctly flipping the 7th bit as per IPv6 spec:
#!/bin/bash
IFS=':'; set $1; unset IFS
printf "fe80::%x%x:%x:%x:%xn" 0x$(( 0x$1 ^ 0x02 )) 0x$2 0x$3ff 0xfe$4 0x$5$6
Example of bit-flipping:
$ mac_to_ipv6 00:00:00:00:00:00
fe80::200:00ff:fe00:0000
add a comment |
You can create a function, that uses IFS
to split the MAC address into 6 colon-separated groups and assembles them. You'll also need to flip the 7th most significant bit (thanks bahamat), i.e. bit 1 of the first byte:
mac_to_ipv6_ll()
IFS=':'; set $1; unset IFS
echo "fe80::$(printf %02x $((0x$1 ^ 2)))$2:$3ff:fe$4:$5$6"
Usage example:
$ mac_to_ipv6_ll 12:34:56:78:90:12
fe80::1034:56ff:fe78:9012
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f79349%2fconvert-mac-address-to-link-local-address-with-bash%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can use IFS
to split the MAC address into 6 colon-separated groups and assemble them. You'll also need to flip the 7th most significant bit (thanks bahamat), i.e. bit 1 of the first byte.
mac_to_ipv6 ()
IFS=':'; set $1; unset IFS
ipv6_address="fe80::$(printf %02x $((0x$1 ^ 2)))$2:$3ff:fe$4:$5$6"
You can use the prefix and suffix stripping constructs $VAR#PREFIX
and $VAR%SUFFIX
to cut the MAC address into pieces.
mac_to_ipv6 ()
mac=$1
ipv6_address=fe80::$(printf %02x $((0x$mac%%:* ^ 2)))
mac=$mac#*:
ipv6_address=$ipv6_address$mac%:*:*:*ff:fe
mac=$mac#*:*:
ipv6_address=$ipv6_address$mac%:*$mac##*:
You can use the substring construct (bash only, not sh).
mac_to_ipv6 ()
local mac=$1 byte0
printf %02x -v byte0 $((0x$mac:0:2 ^ 2))
ipv6_address="fe80::$byte0$mac:3:5ff:fe$mac:9:5$mac:15:2"
This doesn't work because link-local addresses have the 7th bit flipped high.
– bahamat
Jun 14 '13 at 5:35
This doesn't seem to work any more, I posted a working version below
– rubo77
Dec 16 at 8:15
add a comment |
You can use IFS
to split the MAC address into 6 colon-separated groups and assemble them. You'll also need to flip the 7th most significant bit (thanks bahamat), i.e. bit 1 of the first byte.
mac_to_ipv6 ()
IFS=':'; set $1; unset IFS
ipv6_address="fe80::$(printf %02x $((0x$1 ^ 2)))$2:$3ff:fe$4:$5$6"
You can use the prefix and suffix stripping constructs $VAR#PREFIX
and $VAR%SUFFIX
to cut the MAC address into pieces.
mac_to_ipv6 ()
mac=$1
ipv6_address=fe80::$(printf %02x $((0x$mac%%:* ^ 2)))
mac=$mac#*:
ipv6_address=$ipv6_address$mac%:*:*:*ff:fe
mac=$mac#*:*:
ipv6_address=$ipv6_address$mac%:*$mac##*:
You can use the substring construct (bash only, not sh).
mac_to_ipv6 ()
local mac=$1 byte0
printf %02x -v byte0 $((0x$mac:0:2 ^ 2))
ipv6_address="fe80::$byte0$mac:3:5ff:fe$mac:9:5$mac:15:2"
This doesn't work because link-local addresses have the 7th bit flipped high.
– bahamat
Jun 14 '13 at 5:35
This doesn't seem to work any more, I posted a working version below
– rubo77
Dec 16 at 8:15
add a comment |
You can use IFS
to split the MAC address into 6 colon-separated groups and assemble them. You'll also need to flip the 7th most significant bit (thanks bahamat), i.e. bit 1 of the first byte.
mac_to_ipv6 ()
IFS=':'; set $1; unset IFS
ipv6_address="fe80::$(printf %02x $((0x$1 ^ 2)))$2:$3ff:fe$4:$5$6"
You can use the prefix and suffix stripping constructs $VAR#PREFIX
and $VAR%SUFFIX
to cut the MAC address into pieces.
mac_to_ipv6 ()
mac=$1
ipv6_address=fe80::$(printf %02x $((0x$mac%%:* ^ 2)))
mac=$mac#*:
ipv6_address=$ipv6_address$mac%:*:*:*ff:fe
mac=$mac#*:*:
ipv6_address=$ipv6_address$mac%:*$mac##*:
You can use the substring construct (bash only, not sh).
mac_to_ipv6 ()
local mac=$1 byte0
printf %02x -v byte0 $((0x$mac:0:2 ^ 2))
ipv6_address="fe80::$byte0$mac:3:5ff:fe$mac:9:5$mac:15:2"
You can use IFS
to split the MAC address into 6 colon-separated groups and assemble them. You'll also need to flip the 7th most significant bit (thanks bahamat), i.e. bit 1 of the first byte.
mac_to_ipv6 ()
IFS=':'; set $1; unset IFS
ipv6_address="fe80::$(printf %02x $((0x$1 ^ 2)))$2:$3ff:fe$4:$5$6"
You can use the prefix and suffix stripping constructs $VAR#PREFIX
and $VAR%SUFFIX
to cut the MAC address into pieces.
mac_to_ipv6 ()
mac=$1
ipv6_address=fe80::$(printf %02x $((0x$mac%%:* ^ 2)))
mac=$mac#*:
ipv6_address=$ipv6_address$mac%:*:*:*ff:fe
mac=$mac#*:*:
ipv6_address=$ipv6_address$mac%:*$mac##*:
You can use the substring construct (bash only, not sh).
mac_to_ipv6 ()
local mac=$1 byte0
printf %02x -v byte0 $((0x$mac:0:2 ^ 2))
ipv6_address="fe80::$byte0$mac:3:5ff:fe$mac:9:5$mac:15:2"
edited Apr 13 '17 at 12:37
Community♦
1
1
answered Jun 13 '13 at 23:39
Gilles
527k12710561580
527k12710561580
This doesn't work because link-local addresses have the 7th bit flipped high.
– bahamat
Jun 14 '13 at 5:35
This doesn't seem to work any more, I posted a working version below
– rubo77
Dec 16 at 8:15
add a comment |
This doesn't work because link-local addresses have the 7th bit flipped high.
– bahamat
Jun 14 '13 at 5:35
This doesn't seem to work any more, I posted a working version below
– rubo77
Dec 16 at 8:15
This doesn't work because link-local addresses have the 7th bit flipped high.
– bahamat
Jun 14 '13 at 5:35
This doesn't work because link-local addresses have the 7th bit flipped high.
– bahamat
Jun 14 '13 at 5:35
This doesn't seem to work any more, I posted a working version below
– rubo77
Dec 16 at 8:15
This doesn't seem to work any more, I posted a working version below
– rubo77
Dec 16 at 8:15
add a comment |
Taking Gilles' explanation, but correctly flipping the 7th bit as per IPv6 spec:
#!/bin/bash
IFS=':'; set $1; unset IFS
printf "fe80::%x%x:%x:%x:%xn" 0x$(( 0x$1 ^ 0x02 )) 0x$2 0x$3ff 0xfe$4 0x$5$6
Example of bit-flipping:
$ mac_to_ipv6 00:00:00:00:00:00
fe80::200:00ff:fe00:0000
add a comment |
Taking Gilles' explanation, but correctly flipping the 7th bit as per IPv6 spec:
#!/bin/bash
IFS=':'; set $1; unset IFS
printf "fe80::%x%x:%x:%x:%xn" 0x$(( 0x$1 ^ 0x02 )) 0x$2 0x$3ff 0xfe$4 0x$5$6
Example of bit-flipping:
$ mac_to_ipv6 00:00:00:00:00:00
fe80::200:00ff:fe00:0000
add a comment |
Taking Gilles' explanation, but correctly flipping the 7th bit as per IPv6 spec:
#!/bin/bash
IFS=':'; set $1; unset IFS
printf "fe80::%x%x:%x:%x:%xn" 0x$(( 0x$1 ^ 0x02 )) 0x$2 0x$3ff 0xfe$4 0x$5$6
Example of bit-flipping:
$ mac_to_ipv6 00:00:00:00:00:00
fe80::200:00ff:fe00:0000
Taking Gilles' explanation, but correctly flipping the 7th bit as per IPv6 spec:
#!/bin/bash
IFS=':'; set $1; unset IFS
printf "fe80::%x%x:%x:%x:%xn" 0x$(( 0x$1 ^ 0x02 )) 0x$2 0x$3ff 0xfe$4 0x$5$6
Example of bit-flipping:
$ mac_to_ipv6 00:00:00:00:00:00
fe80::200:00ff:fe00:0000
answered Jun 14 '13 at 6:18
bahamat
24.1k14690
24.1k14690
add a comment |
add a comment |
You can create a function, that uses IFS
to split the MAC address into 6 colon-separated groups and assembles them. You'll also need to flip the 7th most significant bit (thanks bahamat), i.e. bit 1 of the first byte:
mac_to_ipv6_ll()
IFS=':'; set $1; unset IFS
echo "fe80::$(printf %02x $((0x$1 ^ 2)))$2:$3ff:fe$4:$5$6"
Usage example:
$ mac_to_ipv6_ll 12:34:56:78:90:12
fe80::1034:56ff:fe78:9012
add a comment |
You can create a function, that uses IFS
to split the MAC address into 6 colon-separated groups and assembles them. You'll also need to flip the 7th most significant bit (thanks bahamat), i.e. bit 1 of the first byte:
mac_to_ipv6_ll()
IFS=':'; set $1; unset IFS
echo "fe80::$(printf %02x $((0x$1 ^ 2)))$2:$3ff:fe$4:$5$6"
Usage example:
$ mac_to_ipv6_ll 12:34:56:78:90:12
fe80::1034:56ff:fe78:9012
add a comment |
You can create a function, that uses IFS
to split the MAC address into 6 colon-separated groups and assembles them. You'll also need to flip the 7th most significant bit (thanks bahamat), i.e. bit 1 of the first byte:
mac_to_ipv6_ll()
IFS=':'; set $1; unset IFS
echo "fe80::$(printf %02x $((0x$1 ^ 2)))$2:$3ff:fe$4:$5$6"
Usage example:
$ mac_to_ipv6_ll 12:34:56:78:90:12
fe80::1034:56ff:fe78:9012
You can create a function, that uses IFS
to split the MAC address into 6 colon-separated groups and assembles them. You'll also need to flip the 7th most significant bit (thanks bahamat), i.e. bit 1 of the first byte:
mac_to_ipv6_ll()
IFS=':'; set $1; unset IFS
echo "fe80::$(printf %02x $((0x$1 ^ 2)))$2:$3ff:fe$4:$5$6"
Usage example:
$ mac_to_ipv6_ll 12:34:56:78:90:12
fe80::1034:56ff:fe78:9012
edited Dec 13 at 16:33
answered Dec 12 at 15:14
rubo77
7,4102469132
7,4102469132
add a comment |
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f79349%2fconvert-mac-address-to-link-local-address-with-bash%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
You also have to flip the universal/local bit.
– Michael Hampton
Jun 14 '13 at 0:08