v-list-dns-template 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #!/bin/bash
  2. # info: list dns template
  3. # options: template [format]
  4. #
  5. # The function for obtaining the DNS template parameters.
  6. #----------------------------------------------------------#
  7. # Variable&Function #
  8. #----------------------------------------------------------#
  9. # Argument defenition
  10. template=$1
  11. format=${2-shell}
  12. # Includes
  13. source $VESTA/func/main.sh
  14. source $VESTA/func/domain.sh
  15. # Json func
  16. json_list_dns() {
  17. echo '{'
  18. fileds_count=$(echo $fields| wc -w )
  19. while read line; do
  20. IFS=$'\n'
  21. eval $line
  22. if [ -n "$data" ]; then
  23. echo -e ' },'
  24. fi
  25. i=1
  26. IFS=' '
  27. for field in $fields; do
  28. eval value=\"$field\"
  29. value=$(echo "$value" | sed -e 's/"/\\"/g' -e "s/%quote%/'/g")
  30. if [ $i -eq 1 ]; then
  31. (( ++i))
  32. echo -e "\t\"$value\": {"
  33. else
  34. if [ $i -lt $fileds_count ]; then
  35. (( ++i))
  36. echo -e "\t\t\"${field//$/}\": \"${value//,/, }\","
  37. else
  38. echo -e "\t\t\"${field//$/}\": \"${value//,/, }\""
  39. data=1
  40. fi
  41. fi
  42. done
  43. done < $conf
  44. if [ -n "$data" ]; then
  45. echo -e ' }'
  46. fi
  47. echo -e '}'
  48. }
  49. # Shell function
  50. shell_list_dns() {
  51. if [ -z "$nohead" ] ; then
  52. echo "${fields//$/}"
  53. for a in $fields; do
  54. echo -e "------ \c"
  55. done
  56. echo
  57. fi
  58. while read line ; do
  59. eval $line
  60. eval echo "$fields" | sed -e "s/%quote%/'/g"
  61. done < $conf
  62. }
  63. #----------------------------------------------------------#
  64. # Verifications #
  65. #----------------------------------------------------------#
  66. check_args '1' "$#" 'template [format]'
  67. validate_format 'template'
  68. is_dns_template_valid
  69. #----------------------------------------------------------#
  70. # Action #
  71. #----------------------------------------------------------#
  72. # Defining config and fields
  73. conf=$DNSTPL/$template.tpl
  74. fields='$RECORD $TYPE $PRIORITY $VALUE'
  75. # Listing templates
  76. case $format in
  77. json) json_list_dns ;;
  78. plain) nohead=1; shell_list_dns ;;
  79. shell) shell_list_dns | column -t ;;
  80. *) check_args '1' '0' 'template [format]';;
  81. esac
  82. #----------------------------------------------------------#
  83. # Vesta #
  84. #----------------------------------------------------------#
  85. exit