v_list_user_ips 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #!/bin/bash
  2. # info: list user ips
  3. # options user [format]
  4. #
  5. # The function for obtainig the list of available ip addresses.
  6. #----------------------------------------------------------#
  7. # Variable&Function #
  8. #----------------------------------------------------------#
  9. # Argument defenition
  10. user=$1
  11. format=${2-shell}
  12. # Importing variables
  13. source $VESTA/conf/vars.conf
  14. source $V_FUNC/shared.func
  15. # Json function
  16. json_list_user_ips() {
  17. # Print top bracket
  18. echo '{'
  19. fileds_count=$(echo "$fields" | wc -w)
  20. for IP in $(ls $V_IPS/); do
  21. source $V_IPS/$IP
  22. if [ "$OWNER" = "$user" ]; then
  23. # Closing bracket if there already was output
  24. if [ -n "$data" ]; then
  25. echo -e ' },'
  26. fi
  27. i=1
  28. for field in $fields; do
  29. eval value=$field
  30. if [ $i -eq 1 ]; then
  31. # Printing parrent
  32. (( ++i))
  33. echo -e "\t\"$value\": {"
  34. else
  35. # Printing child
  36. if [ $i -lt $fileds_count ]; then
  37. (( ++i))
  38. echo -e "\t\t\"${field//$/}\": \"${value//,/, }\","
  39. else
  40. echo -e "\t\t\"${field//$/}\": \"${value//,/, }\""
  41. data=1
  42. fi
  43. fi
  44. done
  45. else
  46. if [ "$OWNER" = 'admin' ] && [ "$STATUS" = 'shared' ]; then
  47. i=1
  48. for field in $fields; do
  49. eval value=$field
  50. if [ $i -eq 1 ]; then
  51. # Printing parrent
  52. (( ++i))
  53. echo -e "\t\"$value\": {"
  54. else
  55. # Printing child
  56. if [ $i -lt $fileds_count ]; then
  57. (( ++i))
  58. echo -e "\t\t\"${field//$/}\": \"${value//,/, }\","
  59. else
  60. echo -e "\t\t\"${field//$/}\": \"${value//,/, }\""
  61. data=1
  62. fi
  63. fi
  64. done
  65. fi
  66. fi
  67. done
  68. # Closing bracket if there was output
  69. if [ -n "$data" ]; then
  70. echo -e ' }'
  71. fi
  72. # Printing bottom bracket
  73. echo -e '}'
  74. }
  75. # Shell function
  76. shell_list_user_ips() {
  77. for IP in $(ls $V_IPS/); do
  78. source $V_IPS/$IP
  79. if [ "$OWNER" = "$user" ]; then
  80. eval echo "$fields"
  81. else
  82. if [ "$OWNER" = 'admin' ] && [ "$STATUS" = 'shared' ]; then
  83. eval echo "$fields"
  84. fi
  85. fi
  86. done
  87. }
  88. #----------------------------------------------------------#
  89. # Verifications #
  90. #----------------------------------------------------------#
  91. # Checking arg number
  92. check_args '1' "$#" 'user [format]'
  93. # Checking argument format
  94. format_validation 'user'
  95. # Checking user
  96. is_user_valid
  97. #----------------------------------------------------------#
  98. # Action #
  99. #----------------------------------------------------------#
  100. # Defining fileds to select
  101. fields='$IP $OWNER $STATUS $NAME'
  102. # Listing ips
  103. case $format in
  104. json) json_list_user_ips ;;
  105. plain) nohead=1; shell_list_user_ips ;;
  106. shell) shell_list_user_ips | column -t ;;
  107. *) check_args '1' '0' 'user [format]' ;;
  108. esac
  109. #----------------------------------------------------------#
  110. # Vesta #
  111. #----------------------------------------------------------#
  112. exit