Convert MAC address to Link-local address with bash

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP












5














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










share|improve this question



















  • 1




    You also have to flip the universal/local bit.
    – Michael Hampton
    Jun 14 '13 at 0:08















5














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










share|improve this question



















  • 1




    You also have to flip the universal/local bit.
    – Michael Hampton
    Jun 14 '13 at 0:08













5












5








5


1





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










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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












  • 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










3 Answers
3






active

oldest

votes


















5














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"






share|improve this answer






















  • 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


















3














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





share|improve this answer




























    0














    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





    share|improve this answer






















      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
      );



      );













      draft saved

      draft discarded


















      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









      5














      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"






      share|improve this answer






















      • 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















      5














      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"






      share|improve this answer






















      • 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













      5












      5








      5






      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"






      share|improve this answer














      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"







      share|improve this answer














      share|improve this answer



      share|improve this answer








      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
















      • 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













      3














      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





      share|improve this answer

























        3














        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





        share|improve this answer























          3












          3








          3






          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





          share|improve this answer












          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






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jun 14 '13 at 6:18









          bahamat

          24.1k14690




          24.1k14690





















              0














              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





              share|improve this answer



























                0














                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





                share|improve this answer

























                  0












                  0








                  0






                  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





                  share|improve this answer














                  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






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Dec 13 at 16:33

























                  answered Dec 12 at 15:14









                  rubo77

                  7,4102469132




                  7,4102469132



























                      draft saved

                      draft discarded
















































                      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.




                      draft saved


                      draft discarded














                      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





















































                      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






                      Popular posts from this blog

                      How to check contact read email or not when send email to Individual?

                      Displaying single band from multi-band raster using QGIS

                      How many registers does an x86_64 CPU actually have?