v-list-firewall-rule 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #!/bin/bash
  2. # info: list firewall rule
  3. # options: RULE [FORMAT]
  4. #
  5. # The function of obtaining firewall rule parameters.
  6. #----------------------------------------------------------#
  7. # Variable&Function #
  8. #----------------------------------------------------------#
  9. # Argument definition
  10. rule=$1
  11. format=${2-shell}
  12. # Includes
  13. source $VESTA/func/main.sh
  14. # Json function
  15. json_list_fw_rule() {
  16. i=1
  17. fileds_count=$(echo "$fields" | wc -w)
  18. line=$(grep "RULE='$rule'" $conf)
  19. echo '{'
  20. eval $line
  21. for field in $fields; do
  22. eval value=$field
  23. if [ "$i" -eq 1 ]; then
  24. echo -e "\t\"$value\": {"
  25. else
  26. if [ "$fileds_count" -eq "$i" ]; then
  27. echo -e "\t\t\"${field//$/}\": \"$value\""
  28. else
  29. echo -e "\t\t\"${field//$/}\": \"$value\","
  30. fi
  31. fi
  32. (( ++i))
  33. done
  34. if [ -n "$value" ]; then
  35. echo -e ' }'
  36. fi
  37. echo -e "}"
  38. }
  39. # Shell function
  40. shell_list_fw_rule() {
  41. line=$(grep "RULE='$rule'" $conf)
  42. eval $line
  43. for field in $fields; do
  44. eval key="$field"
  45. if [ -z "$key" ]; then
  46. key=NULL
  47. fi
  48. echo "${field//$/}: $key "
  49. done
  50. }
  51. #----------------------------------------------------------#
  52. # Verifications #
  53. #----------------------------------------------------------#
  54. check_args '1' "$#" 'RULE [FORMAT]'
  55. is_object_valid '../../data/firewall/rules' 'RULE' "$rule"
  56. #----------------------------------------------------------#
  57. # Action #
  58. #----------------------------------------------------------#
  59. # Defining config and fields to select
  60. conf=$VESTA/data/firewall/rules.conf
  61. fields="\$RULE \$ACTION \$PROTOCOL \$PORT \$IP \$COMMENT"
  62. fields="$fields \$RULE \$SUSPENDED \$TIME \$DATE"
  63. # Listing fw rule
  64. case $format in
  65. json) json_list_fw_rule ;;
  66. plain) nohead=1; shell_list_fw_rule ;;
  67. shell) shell_list_fw_rule |column -t ;;
  68. *) check_args '2' '0' 'RULE [FORMAT]'
  69. esac
  70. #----------------------------------------------------------#
  71. # Vesta #
  72. #----------------------------------------------------------#
  73. exit