美文网首页
sh和bash两种解析器,IPv6简写转换写法

sh和bash两种解析器,IPv6简写转换写法

作者: johney_zhou | 来源:发表于2020-08-12 13:11 被阅读0次

    网上都是关于IPv6补全的,今天我提供一下IPv6完成简写的方法,如下:

    sh解析器:


    #!/bin/sh

    function expand_ipv6() {

        # input: ::

        # output: 0:0:0:0:0:0:0:0

        # input: 2001:2d:1f::1

        # output: 2001:2d:1f:0:0:0:0:1

        local ipv6=${1}

        local colon_num=$(echo ${ipv6} | awk '{print gsub(/:/, "")}')

        local replace_str=""

        local i=0

        while [ $colon_num -lt 7 ]

        do

            replace_str="${replace_str}:0"

            colon_num=`expr $colon_num + 1`

        done

        replace_str="${replace_str}:"

        local ipv6_expanded=${ipv6/::/$replace_str}

        [[ ${ipv6_expanded} == *: ]] && ipv6_expanded="${ipv6_expanded}0"

        [[ ${ipv6_expanded} == :* ]] && ipv6_expanded="0${ipv6_expanded}"

        echo ${ipv6_expanded}

    }

    function expand_expanded_ipv6() {

        # input: 0:0:0:0:0:0:0:0

        # output: 0000:0000:0000:0000:0000:0000:0000:0000

        # input: 2001:2d:1f:0:0:0:0:1

        # output: 2001:002d:001f:0000:0000:0000:0000:0001

        expanded_ipv6=${1}

        x=${expanded_ipv6//:/ }

        set $x

        local j=0

        local hex_arr=""

        for i in "$@"

        do

            j=`expr $j + 1`

            if [ ${#i} -eq 1 ]

            then

                hex_arr="${hex_arr}:000${i}"

                continue

            fi

            if [ ${#i} -eq 2 ]

            then

              hex_arr="${hex_arr}:00${i}"

              continue

            fi

            if [ ${#i} -eq 3 ]

            then

                hex_arr="${hex_arr}:0${i}"

                continue

            fi

            hex_arr="${hex_arr}:${i}"

        done

        echo $hex_arr | sed 's/^.//'

    }

    function simple_ipv6() {

        # input: 2000:002d:001f:0000:0000:0000:0000:0001

        # output: 2000:2d:1f::1

        local expanded_ipv6=${1}

        x=${expanded_ipv6//:/ }

        set $x

        local j=1

        local replace_str=""

        local m=0

        local n=0

        local k=1

        for i in "$@"

        do

            local tmp=$i

            if [ "$tmp"x == "0000"x ] 

            then 

                j=`expr $j + 1`

                eval tmp=\$$j     

                if [ "$tmp"x == "0000"x ]

                then

                    m=$j

                    echo "m is:$m"

                    break

                fi

                j=`expr $j - 1`

            fi

            j=`expr $j + 1`

        done

        j=1

        for i in "$@"                                                   

        do                                                             

            local tmp=$i                                               

            if [ "$tmp"x == "0000"x ]       

            then                                   

                j=`expr $j + 1`         

                eval tmp=\$$j         

                if [ "$tmp"x == "0000"x ]

                then                   

                    j=`expr $j + 1` 

                    eval tmp=\$$j   

                    if [ "$tmp"x != "0000"x ]

                    then                   

                            n=$j           

                            echo "n is:$n"

                            break 

                    fi                     

                    j=`expr $j - 1`                   

                fi                             

                j=`expr $j - 1`                 

            fi                                             

            j=`expr $j + 1`                         

        done

        for i in "$@"

        do

            new_str=$(echo -e $i | sed -r 's/0*([0-9])/\1/')

            tmp1=`expr $m - 1`

            tmp2=`expr $m - 2`

            tmp3=`expr $n - 1`

            if [ $k -lt $tmp1 ]

            then

                replace_str="${replace_str}${new_str}:"

            fi

            if [ $k -eq $tmp2 ]

            then

                replace_str="${replace_str}:"

            fi

            if [ $k -ge $tmp3 ]

            then

                replace_str="${replace_str}${new_str}:"

            fi

            k=`expr $k + 1`

        done

            echo $replace_str | sed 's/.$//'

    }

    expand_ipv6 $1

    expand_expanded_ipv6 $(expand_ipv6 $1)

    simple_ipv6 $(expand_expanded_ipv6 $(expand_ipv6 $1))


    bash解析器:

    #!/usr/bin/env bash

    function expand_ipv6() {

        # input: ::

        # output: 0:0:0:0:0:0:0:0

        # input: 2001:2d:1f::1

        # output: 2001:2d:1f:0:0:0:0:1

        local ipv6=${1}

        local colon_num=$(echo ${ipv6} | awk '{print gsub(/:/, "")}')

        local replace_str=""

        for i in `seq 0 $(( 7 - ${colon_num} ))`;do

    replace_str="${replace_str}:0"

        done

        replace_str="${replace_str}:"

        local ipv6_expanded=${ipv6/::/$replace_str}

        [[ ${ipv6_expanded} == *: ]] && ipv6_expanded="${ipv6_expanded}0"

        [[ ${ipv6_expanded} == :* ]] && ipv6_expanded="0${ipv6_expanded}"

        # return value

        echo ${ipv6_expanded}

    }

    function expand_expanded_ipv6() {

        # input: 0:0:0:0:0:0:0:0

        # output: 0000:0000:0000:0000:0000:0000:0000:0000

        # input: 2001:2d:1f:0:0:0:0:1

        # output: 2001:002d:001f:0000:0000:0000:0000:0001

        local expanded_ipv6=${1}

        local hex_arr=(${expanded_ipv6//:/ })

        for i in `seq 0 8`;do   

    local len=${#hex_arr[i]}

            for (( j = 4; j > ${len}; -- j )); do

                hex_arr[i]="0${hex_arr[i]}"

            done

        done

        # return value

        echo ${hex_arr[@]} | tr " " :

    }

    function simple_ipv6() {

        # input: 0:0:0:0:0:0:0:0

        # output: ::

        # input: 2001:2d:1f:0:0:0:0:1

        # output: 2001:2d:1f::1

        local expanded_ipv6=${1}

        local hex_arr=(${expanded_ipv6//:/ })

        local j=0

        local replace_str=""

        for i in `seq 0 7`;do

            local tmp=${hex_arr[i]}

            if [ "$tmp"x == "0000"x ] 

            then 

                array_tmp[j]=${i}

        echo ${array_tmp[j]}

                ((j++))

            fi

        done

        for m in `seq 0 $(( ${j} - 2 ))`;do

            if [ "$array_tmp[m]"x == "$array_tmp[`expr $m + 1`]"x ]

            then

                break

            fi

        done

        echo "m is:${m}"

        for n in `seq $(( ${m})) 8`;do

            if [ "${hex_arr[n]}" != "0000" ]

            then

                #break

        echo "test!!!!"

        break

            fi

        done

        echo "n is:${n}"

        # return value

        echo ${hex_arr[@]} | tr " " :

        for i in `seq 0 7`;do

    new_str=$(echo -e ${hex_arr[i]} | sed -r 's/0*([0-9])/\1/')

    if [ $i -lt `expr $m` ]

    then

        replace_str="${replace_str}${new_str}:"

    fi

    if [ $i -eq `expr $m - 1` ]

            then

                replace_str="${replace_str}:"

            fi

    if [ $i -ge $n  ]

            then

                replace_str="${replace_str}${new_str}:"

            fi

    echo $replace_str | sed 's/.$//'

    done

    }

    expand_ipv6 $1

    expand_expanded_ipv6 $(expand_ipv6 $1)

    simple_ipv6 $(expand_expanded_ipv6 $(expand_ipv6 $1))


    测试结果如下:

    相关文章

      网友评论

          本文标题:sh和bash两种解析器,IPv6简写转换写法

          本文链接:https://www.haomeiwen.com/subject/lkipdktx.html