How to use shell to derive an IPv6 address from a MAC address?












10














We know that we can use the MAC address to create an interface identifier, e.g. for a link-local IPv6 address which should be unique in the Network.



The image shows the way to do this:



Create interface identifier from MAC address



My questions are:




  • How can I create an IPv6 address from a MAC using awk or sed?

  • OR is there any command that gives me the link-local IPv6 address for a specific MAC (something like that createIPv6 myMAC)?










share|improve this question
























  • I rephrased your question a bit (pending review). IPv6 addresses are not extracted from MAC addresses, but created. And here you deal only with the interface identifier part (the last 64 bit) of an IPv6 address. But I'm not sure: Do you only want the interface identifier or a whole IPv6 address? Please re-edit, if I did not understand you correctly.
    – Dubu
    Jun 12 '14 at 7:34












  • @Dubu, Thanks for editing, I want the whole IPv6 address.
    – Networker
    Jun 12 '14 at 11:13
















10














We know that we can use the MAC address to create an interface identifier, e.g. for a link-local IPv6 address which should be unique in the Network.



The image shows the way to do this:



Create interface identifier from MAC address



My questions are:




  • How can I create an IPv6 address from a MAC using awk or sed?

  • OR is there any command that gives me the link-local IPv6 address for a specific MAC (something like that createIPv6 myMAC)?










share|improve this question
























  • I rephrased your question a bit (pending review). IPv6 addresses are not extracted from MAC addresses, but created. And here you deal only with the interface identifier part (the last 64 bit) of an IPv6 address. But I'm not sure: Do you only want the interface identifier or a whole IPv6 address? Please re-edit, if I did not understand you correctly.
    – Dubu
    Jun 12 '14 at 7:34












  • @Dubu, Thanks for editing, I want the whole IPv6 address.
    – Networker
    Jun 12 '14 at 11:13














10












10








10


4





We know that we can use the MAC address to create an interface identifier, e.g. for a link-local IPv6 address which should be unique in the Network.



The image shows the way to do this:



Create interface identifier from MAC address



My questions are:




  • How can I create an IPv6 address from a MAC using awk or sed?

  • OR is there any command that gives me the link-local IPv6 address for a specific MAC (something like that createIPv6 myMAC)?










share|improve this question















We know that we can use the MAC address to create an interface identifier, e.g. for a link-local IPv6 address which should be unique in the Network.



The image shows the way to do this:



Create interface identifier from MAC address



My questions are:




  • How can I create an IPv6 address from a MAC using awk or sed?

  • OR is there any command that gives me the link-local IPv6 address for a specific MAC (something like that createIPv6 myMAC)?







sed awk ipv6 mac-address






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Aug 6 '14 at 5:28

























asked Jun 11 '14 at 22:10









Networker

5,929103867




5,929103867












  • I rephrased your question a bit (pending review). IPv6 addresses are not extracted from MAC addresses, but created. And here you deal only with the interface identifier part (the last 64 bit) of an IPv6 address. But I'm not sure: Do you only want the interface identifier or a whole IPv6 address? Please re-edit, if I did not understand you correctly.
    – Dubu
    Jun 12 '14 at 7:34












  • @Dubu, Thanks for editing, I want the whole IPv6 address.
    – Networker
    Jun 12 '14 at 11:13


















  • I rephrased your question a bit (pending review). IPv6 addresses are not extracted from MAC addresses, but created. And here you deal only with the interface identifier part (the last 64 bit) of an IPv6 address. But I'm not sure: Do you only want the interface identifier or a whole IPv6 address? Please re-edit, if I did not understand you correctly.
    – Dubu
    Jun 12 '14 at 7:34












  • @Dubu, Thanks for editing, I want the whole IPv6 address.
    – Networker
    Jun 12 '14 at 11:13
















I rephrased your question a bit (pending review). IPv6 addresses are not extracted from MAC addresses, but created. And here you deal only with the interface identifier part (the last 64 bit) of an IPv6 address. But I'm not sure: Do you only want the interface identifier or a whole IPv6 address? Please re-edit, if I did not understand you correctly.
– Dubu
Jun 12 '14 at 7:34






I rephrased your question a bit (pending review). IPv6 addresses are not extracted from MAC addresses, but created. And here you deal only with the interface identifier part (the last 64 bit) of an IPv6 address. But I'm not sure: Do you only want the interface identifier or a whole IPv6 address? Please re-edit, if I did not understand you correctly.
– Dubu
Jun 12 '14 at 7:34














@Dubu, Thanks for editing, I want the whole IPv6 address.
– Networker
Jun 12 '14 at 11:13




@Dubu, Thanks for editing, I want the whole IPv6 address.
– Networker
Jun 12 '14 at 11:13










4 Answers
4






active

oldest

votes


















10














If you want to create a whole IPv6 address from a MAC (and a given prefix), you could use the excellent ipv6calc tool by Peter Bieringer.



The following command creates a link-local IPv6 address (fe80:: prefix) from a MAC address:



$ ipv6calc --action prefixmac2ipv6 --in prefix+mac --out ipv6addr fe80:: 00:21:5b:f7:25:1b
fe80::221:5bff:fef7:251b


You can leave most of the options away and let the command guess what to do:



$ ipv6calc --in prefix+mac fe80:: 00:21:5b:f7:25:1b
No action type specified, try autodetection...found type: prefixmac2ipv6
fe80::221:5bff:fef7:251b


For Debian distros, ipv6calc is in the main repository.






share|improve this answer





























    4














    From the IPv6 Wikipedia entry a more textual description:




    A 64-bit interface identifier is most commonly derived from its 48-bit MAC address.



    A MAC address 00:0C:29:0C:47:D5 is turned into a 64-bit EUI-64 by inserting FF:FE in the middle: 00:0C:29:FF:FE:0C:47:D5.




    So replacing the third : with :FF:FE: should do the trick:



    echo  00:0C:29:0C:47:D5 | sed s/:/:FF:FE:/3
    00:0C:29:FF:FE:0C:47:D5


    No idea if that syntax is specific to GNU sed.





    Work in progress:



    Convert that to bits:



    for HEX in $(tr ":" " " <<< 00:0C:29:FF:FE:0C:47:D5) 
    do
    printf "%08d " $(bc <<< "ibase=16;obase=2;$HEX")
    done


    should result in the bits 00000000 00001100 00101001 11111111 11111110 00001100 01000111 11010101 leaving only the flipping of bit number 7.






    share|improve this answer



















    • 1




      what about reverse the 7-th bit !
      – Networker
      Jun 11 '14 at 22:52










    • Ah the U/L bit, good point!
      – HBruijn
      Jun 11 '14 at 23:04



















    3














    #! /usr/bin/env python
    import sys
    n=[int(x, 16) for x in sys.argv[1].split(":")]
    print "fe80::%02x%02x:%02xff:fe%02x:%02x%02x" % tuple([n[0]^2]+n[1:])





    share|improve this answer































      0














      You can create a bash function (and place it in your ~/.bashrc) 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, 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:${3}ff: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%2f136674%2fhow-to-use-shell-to-derive-an-ipv6-address-from-a-mac-address%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        4 Answers
        4






        active

        oldest

        votes








        4 Answers
        4






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        10














        If you want to create a whole IPv6 address from a MAC (and a given prefix), you could use the excellent ipv6calc tool by Peter Bieringer.



        The following command creates a link-local IPv6 address (fe80:: prefix) from a MAC address:



        $ ipv6calc --action prefixmac2ipv6 --in prefix+mac --out ipv6addr fe80:: 00:21:5b:f7:25:1b
        fe80::221:5bff:fef7:251b


        You can leave most of the options away and let the command guess what to do:



        $ ipv6calc --in prefix+mac fe80:: 00:21:5b:f7:25:1b
        No action type specified, try autodetection...found type: prefixmac2ipv6
        fe80::221:5bff:fef7:251b


        For Debian distros, ipv6calc is in the main repository.






        share|improve this answer


























          10














          If you want to create a whole IPv6 address from a MAC (and a given prefix), you could use the excellent ipv6calc tool by Peter Bieringer.



          The following command creates a link-local IPv6 address (fe80:: prefix) from a MAC address:



          $ ipv6calc --action prefixmac2ipv6 --in prefix+mac --out ipv6addr fe80:: 00:21:5b:f7:25:1b
          fe80::221:5bff:fef7:251b


          You can leave most of the options away and let the command guess what to do:



          $ ipv6calc --in prefix+mac fe80:: 00:21:5b:f7:25:1b
          No action type specified, try autodetection...found type: prefixmac2ipv6
          fe80::221:5bff:fef7:251b


          For Debian distros, ipv6calc is in the main repository.






          share|improve this answer
























            10












            10








            10






            If you want to create a whole IPv6 address from a MAC (and a given prefix), you could use the excellent ipv6calc tool by Peter Bieringer.



            The following command creates a link-local IPv6 address (fe80:: prefix) from a MAC address:



            $ ipv6calc --action prefixmac2ipv6 --in prefix+mac --out ipv6addr fe80:: 00:21:5b:f7:25:1b
            fe80::221:5bff:fef7:251b


            You can leave most of the options away and let the command guess what to do:



            $ ipv6calc --in prefix+mac fe80:: 00:21:5b:f7:25:1b
            No action type specified, try autodetection...found type: prefixmac2ipv6
            fe80::221:5bff:fef7:251b


            For Debian distros, ipv6calc is in the main repository.






            share|improve this answer












            If you want to create a whole IPv6 address from a MAC (and a given prefix), you could use the excellent ipv6calc tool by Peter Bieringer.



            The following command creates a link-local IPv6 address (fe80:: prefix) from a MAC address:



            $ ipv6calc --action prefixmac2ipv6 --in prefix+mac --out ipv6addr fe80:: 00:21:5b:f7:25:1b
            fe80::221:5bff:fef7:251b


            You can leave most of the options away and let the command guess what to do:



            $ ipv6calc --in prefix+mac fe80:: 00:21:5b:f7:25:1b
            No action type specified, try autodetection...found type: prefixmac2ipv6
            fe80::221:5bff:fef7:251b


            For Debian distros, ipv6calc is in the main repository.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Jun 12 '14 at 7:45









            Dubu

            2,3951123




            2,3951123

























                4














                From the IPv6 Wikipedia entry a more textual description:




                A 64-bit interface identifier is most commonly derived from its 48-bit MAC address.



                A MAC address 00:0C:29:0C:47:D5 is turned into a 64-bit EUI-64 by inserting FF:FE in the middle: 00:0C:29:FF:FE:0C:47:D5.




                So replacing the third : with :FF:FE: should do the trick:



                echo  00:0C:29:0C:47:D5 | sed s/:/:FF:FE:/3
                00:0C:29:FF:FE:0C:47:D5


                No idea if that syntax is specific to GNU sed.





                Work in progress:



                Convert that to bits:



                for HEX in $(tr ":" " " <<< 00:0C:29:FF:FE:0C:47:D5) 
                do
                printf "%08d " $(bc <<< "ibase=16;obase=2;$HEX")
                done


                should result in the bits 00000000 00001100 00101001 11111111 11111110 00001100 01000111 11010101 leaving only the flipping of bit number 7.






                share|improve this answer



















                • 1




                  what about reverse the 7-th bit !
                  – Networker
                  Jun 11 '14 at 22:52










                • Ah the U/L bit, good point!
                  – HBruijn
                  Jun 11 '14 at 23:04
















                4














                From the IPv6 Wikipedia entry a more textual description:




                A 64-bit interface identifier is most commonly derived from its 48-bit MAC address.



                A MAC address 00:0C:29:0C:47:D5 is turned into a 64-bit EUI-64 by inserting FF:FE in the middle: 00:0C:29:FF:FE:0C:47:D5.




                So replacing the third : with :FF:FE: should do the trick:



                echo  00:0C:29:0C:47:D5 | sed s/:/:FF:FE:/3
                00:0C:29:FF:FE:0C:47:D5


                No idea if that syntax is specific to GNU sed.





                Work in progress:



                Convert that to bits:



                for HEX in $(tr ":" " " <<< 00:0C:29:FF:FE:0C:47:D5) 
                do
                printf "%08d " $(bc <<< "ibase=16;obase=2;$HEX")
                done


                should result in the bits 00000000 00001100 00101001 11111111 11111110 00001100 01000111 11010101 leaving only the flipping of bit number 7.






                share|improve this answer



















                • 1




                  what about reverse the 7-th bit !
                  – Networker
                  Jun 11 '14 at 22:52










                • Ah the U/L bit, good point!
                  – HBruijn
                  Jun 11 '14 at 23:04














                4












                4








                4






                From the IPv6 Wikipedia entry a more textual description:




                A 64-bit interface identifier is most commonly derived from its 48-bit MAC address.



                A MAC address 00:0C:29:0C:47:D5 is turned into a 64-bit EUI-64 by inserting FF:FE in the middle: 00:0C:29:FF:FE:0C:47:D5.




                So replacing the third : with :FF:FE: should do the trick:



                echo  00:0C:29:0C:47:D5 | sed s/:/:FF:FE:/3
                00:0C:29:FF:FE:0C:47:D5


                No idea if that syntax is specific to GNU sed.





                Work in progress:



                Convert that to bits:



                for HEX in $(tr ":" " " <<< 00:0C:29:FF:FE:0C:47:D5) 
                do
                printf "%08d " $(bc <<< "ibase=16;obase=2;$HEX")
                done


                should result in the bits 00000000 00001100 00101001 11111111 11111110 00001100 01000111 11010101 leaving only the flipping of bit number 7.






                share|improve this answer














                From the IPv6 Wikipedia entry a more textual description:




                A 64-bit interface identifier is most commonly derived from its 48-bit MAC address.



                A MAC address 00:0C:29:0C:47:D5 is turned into a 64-bit EUI-64 by inserting FF:FE in the middle: 00:0C:29:FF:FE:0C:47:D5.




                So replacing the third : with :FF:FE: should do the trick:



                echo  00:0C:29:0C:47:D5 | sed s/:/:FF:FE:/3
                00:0C:29:FF:FE:0C:47:D5


                No idea if that syntax is specific to GNU sed.





                Work in progress:



                Convert that to bits:



                for HEX in $(tr ":" " " <<< 00:0C:29:FF:FE:0C:47:D5) 
                do
                printf "%08d " $(bc <<< "ibase=16;obase=2;$HEX")
                done


                should result in the bits 00000000 00001100 00101001 11111111 11111110 00001100 01000111 11010101 leaving only the flipping of bit number 7.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Jun 12 '14 at 0:20

























                answered Jun 11 '14 at 22:51









                HBruijn

                5,4861525




                5,4861525








                • 1




                  what about reverse the 7-th bit !
                  – Networker
                  Jun 11 '14 at 22:52










                • Ah the U/L bit, good point!
                  – HBruijn
                  Jun 11 '14 at 23:04














                • 1




                  what about reverse the 7-th bit !
                  – Networker
                  Jun 11 '14 at 22:52










                • Ah the U/L bit, good point!
                  – HBruijn
                  Jun 11 '14 at 23:04








                1




                1




                what about reverse the 7-th bit !
                – Networker
                Jun 11 '14 at 22:52




                what about reverse the 7-th bit !
                – Networker
                Jun 11 '14 at 22:52












                Ah the U/L bit, good point!
                – HBruijn
                Jun 11 '14 at 23:04




                Ah the U/L bit, good point!
                – HBruijn
                Jun 11 '14 at 23:04











                3














                #! /usr/bin/env python
                import sys
                n=[int(x, 16) for x in sys.argv[1].split(":")]
                print "fe80::%02x%02x:%02xff:fe%02x:%02x%02x" % tuple([n[0]^2]+n[1:])





                share|improve this answer




























                  3














                  #! /usr/bin/env python
                  import sys
                  n=[int(x, 16) for x in sys.argv[1].split(":")]
                  print "fe80::%02x%02x:%02xff:fe%02x:%02x%02x" % tuple([n[0]^2]+n[1:])





                  share|improve this answer


























                    3












                    3








                    3






                    #! /usr/bin/env python
                    import sys
                    n=[int(x, 16) for x in sys.argv[1].split(":")]
                    print "fe80::%02x%02x:%02xff:fe%02x:%02x%02x" % tuple([n[0]^2]+n[1:])





                    share|improve this answer














                    #! /usr/bin/env python
                    import sys
                    n=[int(x, 16) for x in sys.argv[1].split(":")]
                    print "fe80::%02x%02x:%02xff:fe%02x:%02x%02x" % tuple([n[0]^2]+n[1:])






                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Jul 9 '15 at 18:58

























                    answered Jul 9 '15 at 18:25









                    Martin Wilck

                    463




                    463























                        0














                        You can create a bash function (and place it in your ~/.bashrc) 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, 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:${3}ff: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 bash function (and place it in your ~/.bashrc) 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, 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:${3}ff: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 bash function (and place it in your ~/.bashrc) 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, 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:${3}ff: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 bash function (and place it in your ~/.bashrc) 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, 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:${3}ff: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










                            answered Dec 16 at 8:45









                            rubo77

                            7,4202569133




                            7,4202569133






























                                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%2f136674%2fhow-to-use-shell-to-derive-an-ipv6-address-from-a-mac-address%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

                                Morgemoulin

                                Scott Moir

                                Souastre