shellipv6

expand ipv6 address in shell script


I'd like to update a djbdns (dbndns) configuration file based on a given IPv6 address, e.g. 2a01:488:66:1000:523:f116:0:1 or ::1.

dbndns requires expanded IPv6 addresses, e.g. 2a010488006610000523f11600000001 for 2a01:488:66:1000:523:f116:0:1.

What's the most simple way to expand such an IPv6 address?


Solution

  • Using sipcalc might do it. It gives more information than you need, but a bit of grep and cut can solve that :-)

    $ EXPANDED=`sipcalc 2001::1 | fgrep Expanded | cut -d '-' -f 2`
    $ echo $EXPAND
    2001:0000:0000:0000:0000:0000:0000:0001
    

    For reference, this is the full output of sipcalc:

    $ sipcalc 2001::1
    -[ipv6 : 2001::1] - 0
    
    [IPV6 INFO]
    Expanded Address        - 2001:0000:0000:0000:0000:0000:0000:0001
    Compressed address      - 2001::1
    Subnet prefix (masked)  - 2001:0:0:0:0:0:0:1/128
    Address ID (masked)     - 0:0:0:0:0:0:0:0/128
    Prefix address          - ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff
    Prefix length           - 128
    Address type            - Aggregatable Global Unicast Addresses
    Network range           - 2001:0000:0000:0000:0000:0000:0000:0001 -
                              2001:0000:0000:0000:0000:0000:0000:0001