1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
| #!/usr/bin/env bash function expand_ipv6() { 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}" echo ${ipv6_expanded} }
function expand_expanded_ipv6() { 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 echo ${hex_arr[@]} | tr " " : }
function convert_ipv6_to_decimal_basing_8bit() { 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 echo ${dec_arr[@]} | tr " " . }
function convert_ipv6_to_decimal_basing_16bit() { 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 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)
|