expand6.sh 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #!/bin/bash
  2. ##################################################################################
  3. #
  4. # Copyright (C) 2017 Craig Miller
  5. #
  6. # See the file "LICENSE" for information on usage and redistribution
  7. # of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  8. # Distributed under GPLv2 License
  9. #
  10. ##################################################################################
  11. # IPv6 Address Expansion functions
  12. #
  13. # by Craig Miller 19 Feb 2017
  14. #
  15. # 16 Nov 2017 v0.93 - added CLI functionality
  16. VERSION=0.93
  17. empty_addr="0000:0000:0000:0000:0000:0000:0000:0000"
  18. empty_addr_len=${#empty_addr}
  19. function usage {
  20. echo " $0 - expand compressed IPv6 addresss "
  21. echo " e.g. $0 2001:db8:1:12:123::456 "
  22. echo " "
  23. echo " -t self test"
  24. echo " "
  25. echo " By Craig Miller - Version: $VERSION"
  26. exit 1
  27. }
  28. if [ "$1" == "-h" ]; then
  29. #call help
  30. usage
  31. fi
  32. #
  33. # Expands IPv6 quibble to 4 digits with leading zeros e.g. db8 -> 0db8
  34. #
  35. # Returns string with expanded quibble
  36. function expand_quibble() {
  37. addr=$1
  38. # create array of quibbles
  39. addr_array=(${addr//:/ })
  40. addr_array_len=${#addr_array[@]}
  41. # step thru quibbles
  42. for ((i=0; i< $addr_array_len ; i++ ))
  43. do
  44. quibble=${addr_array[$i]}
  45. quibble_len=${#quibble}
  46. case $quibble_len in
  47. 1) quibble="000$quibble";;
  48. 2) quibble="00$quibble";;
  49. 3) quibble="0$quibble";;
  50. esac
  51. addr_array[$i]=$quibble
  52. done
  53. # reconstruct addr from quibbles
  54. return_str=${addr_array[*]}
  55. return_str="${return_str// /:}"
  56. echo $return_str
  57. }
  58. #
  59. # Expands IPv6 address :: format to full zeros
  60. #
  61. # Returns string with expanded address
  62. function expand() {
  63. if [[ $1 == *"::"* ]]; then
  64. # check for leading zeros on front_addr
  65. if [[ $1 == "::"* ]]; then
  66. front_addr=0
  67. else
  68. front_addr=$(echo $1 | sed -r 's;([^ ]+)::.*;\1;')
  69. fi
  70. # check for trailing zeros on back_addr
  71. if [[ $1 == *"::" ]]; then
  72. back_addr=0
  73. else
  74. back_addr=$(echo $1 | sed -r 's;.*::([^ ]+);\1;')
  75. fi
  76. front_addr=$(expand_quibble $front_addr)
  77. back_addr=$(expand_quibble $back_addr)
  78. new_addr=$empty_addr
  79. front_addr_len=${#front_addr}
  80. back_addr_len=${#back_addr}
  81. # calculate fill needed
  82. num_zeros=$(($empty_addr_len - $front_addr_len - $back_addr_len - 1))
  83. #fill_str=${empty_addr[0]:0:$num_zeros}
  84. new_addr="$front_addr:${empty_addr[0]:0:$num_zeros}$back_addr"
  85. # return expanded address
  86. echo $new_addr
  87. else
  88. # return input with expandd quibbles
  89. expand_quibble $1
  90. fi
  91. }
  92. # self test - call with '-t' parameter
  93. if [ "$1" == "-t" ]; then
  94. # add address examples to test
  95. expand fd11::1d70:cf84:18ef:d056
  96. expand 2a01::1
  97. expand fe80::f203:8cff:fe3f:f041
  98. expand 2001:db8:123::5
  99. expand 2001:470:ebbd:0:f203:8cff:fe3f:f041
  100. # special cases
  101. expand ::1
  102. expand fd32:197d:3022:1101::
  103. exit 1
  104. fi
  105. # allow script to be sourced (with no arguements)
  106. if [[ $1 != "" ]]; then
  107. # validate input is an IPv6 address
  108. if [[ $1 == *":"* ]]; then
  109. expand $1
  110. else
  111. echo "ERROR: unregcognized IPv6 address $1"
  112. exit 1
  113. fi
  114. fi