Shell 扩展 IPV6

简介: 缩略 IPv6 扩展
#!/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 = 0; i <= $(( 7 - ${colon_num} )); ++ i )); 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 = 0; i < 8; ++ i )); 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 convert_ipv6_to_decimal_basing_8bit() {
    # input(hexadecimal): 0000:0000:0000:0000:0000:0000:0000:0000
    # output(decimal): 00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00
    # input(hexadecimal): 2001:002d:001f:0000:0000:0000:0000:0001
    # output(decimal): 32.1.0.45.0.31.0.0.0.0.0.0.0.0.0.1
    local completed_ipv6=${1}
    local hex_arr=(${completed_ipv6//:/ })
    local hex_arr_split_by_8bit=()
    for (( i = 0; i < 8; ++ i )); do
        hex_arr_split_by_8bit=( ${hex_arr_split_by_8bit[@]} ${hex_arr[i]:0:2} ${hex_arr[i]:2} )
    done
    local dec_arr=()
    for (( i = 0; i < 16; ++ i )); do
        dec_arr=( ${dec_arr[@]} $(echo $(( 16#${hex_arr_split_by_8bit[i]} ))) )
    done
    # return value
    echo ${dec_arr[@]} | tr " " .
}

function convert_ipv6_to_decimal_basing_16bit() {
    # input(hexadecimal): 0:0:0:0:0:0:0:0
    # output(decimal): 0.0.0.0.0.0.0.0
    # input(hexadecimal): 2001:2d:1f:0:0:0:0:1
    # output(decimal): 8193.45.31.0.0.0.0.1
    local expanded_ipv6=${1}
    local hex_arr=(${expanded_ipv6//:/ })
    local dec_arr=()
    for (( i = 0; i < 8; ++ i )); do
        dec_arr=( ${dec_arr[@]} $(echo $(( 16#${hex_arr[i]} ))) )
    done
    # return value
    echo ${dec_arr[@]} | tr " " .
}

expand_ipv6 $1
expand_expanded_ipv6 $(expand_ipv6 $1)
convert_ipv6_to_decimal_basing_8bit $(expand_expanded_ipv6 $(expand_ipv6 $1))
convert_ipv6_to_decimal_basing_16bit $(expand_ipv6 $1)
相关文章
|
1月前
|
存储 算法 Shell
【Shell 命令集合 扩展命令】Linux cksum 命令使用教程
【Shell 命令集合 扩展命令】Linux cksum 命令使用教程
31 0
|
1月前
|
存储 Unix Linux
【Shell 命令集合 扩展命令】Linux chgrp命令使用教程
【Shell 命令集合 扩展命令】Linux chgrp命令使用教程
34 1
|
1月前
|
存储 Shell Linux
【Shell 命令集合 扩展命令】Linux chattr命令使用教程
【Shell 命令集合 扩展命令】Linux chattr命令使用教程
32 1
|
1月前
|
存储 安全 Shell
【Shell 命令集合 文件管理】Linux显示和修改文件或目录的扩展属性 lsattr命令使用教程
【Shell 命令集合 文件管理】Linux显示和修改文件或目录的扩展属性 lsattr命令使用教程
44 0
|
1月前
|
存储 Shell Linux
【Shell 命令集合 扩展命令】Linux cmp命令使用教程
【Shell 命令集合 扩展命令】Linux cmp命令使用教程
25 0
|
3月前
|
Shell Linux
Shell编程自动化之特殊Shell扩展变量
本文主要介绍了Shell编程自动化之特殊Shell扩展变量,并结合实例测试。
104 0
|
9月前
|
Shell PHP Windows
php交互式命令行工具window操作系统安装readline扩展函数实现interactive mode enabled到Interactive Shell
php交互式命令行工具window操作系统安装readline扩展函数实现interactive mode enabled到Interactive Shell
61 0
|
前端开发 Shell 开发工具