iptools.sh 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. #!/bin/sh
  2. ##############################################################################
  3. #
  4. # This program is free software; you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License version 2 as
  6. # published by the Free Software Foundation.
  7. #
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. #
  13. # Copyright (C) 2016 Eric Luehrsen
  14. #
  15. ##############################################################################
  16. #
  17. # These are iptools that might be useful in a larger package, if provided
  18. # elsewhere for common use. One example that many may find useful is turning
  19. # flexible IPV6 colon dividers into PTR. Otherwise these are incomplete and
  20. # would need robustness improvements for more generic applications.
  21. #
  22. ##############################################################################
  23. domain_ptr_ip6() {
  24. # Get the nibble rounded /CIDR ...ip6.arpa.
  25. echo "$1" | awk -F: \
  26. 'BEGIN { OFS = "" ; }
  27. { CIDR = $0 ;
  28. sub(/.*\//,"",CIDR) ;
  29. CIDR = (CIDR / 4) ;
  30. sub(/\/[0-9]+/,"",$0) ;
  31. ct_stop = 9 - NF ;
  32. for(i=1; i<=NF; i++) {
  33. if(length($i) == 0) {
  34. for(j=1; j<=ct_stop; j++) { $i = ($i "0000") ; } }
  35. else { $i = substr(("0000" $i), length($i)+5-4) ; } } ;
  36. y = $0 ;
  37. ct_start = length(y) - 32 + CIDR ;
  38. for(i=ct_start; i>0; i--) { x = (x substr(y,i,1)) ; } ;
  39. gsub(/./,"&\.",x) ;
  40. x = (x "ip6.arpa") ;
  41. print x }'
  42. }
  43. ##############################################################################
  44. host_ptr_ip6() {
  45. # Get complete host ...ip6.arpa.
  46. echo "$1" | awk -F: \
  47. 'BEGIN { OFS = "" ; }
  48. { sub(/\/[0-9]+/,"",$0) ;
  49. ct_stop = 9 - NF ;
  50. for(i=1; i<=NF; i++) {
  51. if(length($i) == 0) {
  52. for(j=1; j<=ct_stop; j++) { $i = ($i "0000") ; } }
  53. else { $i = substr(("0000" $i), length($i)+5-4) ; } } ;
  54. y = $0 ;
  55. ct_start = length(y);
  56. for(i=ct_start; i>0; i--) { x = (x substr(y,i,1)) ; } ;
  57. sub(/[0-9]+\//,"",x) ;
  58. gsub(/./,"&\.",x) ;
  59. x = (x "ip6.arpa") ;
  60. print x }'
  61. }
  62. ##############################################################################
  63. domain_ptr_ip4() {
  64. # Get the byte rounded /CIDR ...in-addr.arpa.
  65. echo "$1" | awk \
  66. '{ CIDR = $0 ;
  67. sub(/.*\//,"",CIDR) ;
  68. CIDR = (CIDR / 8) ;
  69. dtxt = $0 ;
  70. sub(/\/.*/,"",dtxt) ;
  71. split(dtxt, dtxt, ".") ;
  72. for(i=1; i<=CIDR; i++) { x = (dtxt[i] "." x) ; }
  73. x = (x "in-addr.arpa") ;
  74. print x }'
  75. }
  76. ##############################################################################
  77. host_ptr_ip4() {
  78. # Get omplete host ...in-addr.arpa.
  79. echo "$1" | awk -F. \
  80. '{ x = ( $4"."$3"."$2"."$1".in-addr.arpa" ) ;
  81. sub(/\/[0-9]+/,"",x) ;
  82. print x }'
  83. }
  84. ##############################################################################
  85. valid_subnet6() {
  86. case "$1" in
  87. # GA
  88. [1-9][0-9a-f][0-9a-f][0-9a-f]":"*) echo "ok" ;;
  89. # ULA
  90. f[cd][0-9a-f][0-9a-f]":"*) echo "ok" ;;
  91. # fe80::, ::1, and such
  92. *) echo "not" ;;
  93. esac
  94. }
  95. ##############################################################################
  96. valid_subnet4() {
  97. case "$1" in
  98. # Link, Local, and Such
  99. 169"."254"."*) echo "not" ;;
  100. 127"."*) echo "not" ;;
  101. 0"."*) echo "not" ;;
  102. 255"."*) echo "not" ;;
  103. # Other Normal
  104. 25[0-4]"."[0-9]*) echo "ok" ;;
  105. 2[0-4][0-9]"."[0-9]*) echo "ok" ;;
  106. 1[0-9][0-9]"."[0-9]*) echo "ok" ;;
  107. [0-9][0-9]"."[0-9]*) echo "ok" ;;
  108. [0-9]"."[0-9]*) echo "ok" ;;
  109. # Not Right
  110. *) echo "not";;
  111. esac
  112. }
  113. ##############################################################################
  114. private_subnet() {
  115. case "$1" in
  116. 10"."*) echo "ok" ;;
  117. 172"."1[6-9]"."*) echo "ok" ;;
  118. 172"."2[0-9]"."*) echo "ok" ;;
  119. 172"."3[0-1]"."*) echo "ok" ;;
  120. 192"."168"."*) echo "ok" ;;
  121. f[cd][0-9a-f][0-9a-f]":"*) echo "ok" ;;
  122. *) echo "not" ;;
  123. esac
  124. }
  125. ##############################################################################