v_list_user_ips 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. eval echo "$fields"
  24. # Closing bracket if there already was output
  25. if [ -n "$data" ]; then
  26. echo -e ' },'
  27. fi
  28. i=1
  29. for field in $fields; do
  30. eval value=$field
  31. if [ $i -eq 1 ]; then
  32. # Printing parrent
  33. (( ++i))
  34. echo -e "\t\"$value\": {"
  35. else
  36. # Printing child
  37. if [ $i -lt $fileds_count ]; then
  38. (( ++i))
  39. echo -e "\t\t\"${field//$/}\": \"${value//,/, }\","
  40. else
  41. echo -e "\t\t\"${field//$/}\": \"${value//,/, }\""
  42. data=1
  43. fi
  44. fi
  45. done
  46. else
  47. if [ "$OWNER" = 'admin' ] && [ "$STATUS" = 'shared' ]; then
  48. i=1
  49. for field in $fields; do
  50. eval value=$field
  51. if [ $i -eq 1 ]; then
  52. # Printing parrent
  53. (( ++i))
  54. echo -e "\t\"$value\": {"
  55. else
  56. # Printing child
  57. if [ $i -lt $fileds_count ]; then
  58. (( ++i))
  59. echo -e "\t\t\"${field//$/}\": \"${value//,/, }\","
  60. else
  61. echo -e "\t\t\"${field//$/}\": \"${value//,/, }\""
  62. data=1
  63. fi
  64. fi
  65. done
  66. fi
  67. fi
  68. done
  69. # Closing bracket if there was output
  70. if [ -n "$data" ]; then
  71. echo -e ' }'
  72. fi
  73. # Printing bottom bracket
  74. echo -e '}'
  75. }
  76. # Shell function
  77. shell_list_user_ips() {
  78. for IP in $(ls $V_IPS/); do
  79. source $V_IPS/$IP
  80. if [ "$OWNER" = "$user" ]; then
  81. eval echo "$fields"
  82. else
  83. if [ "$OWNER" = 'admin' ] && [ "$STATUS" = 'shared' ]; then
  84. eval echo "$fields"
  85. fi
  86. fi
  87. done
  88. }
  89. #----------------------------------------------------------#
  90. # Verifications #
  91. #----------------------------------------------------------#
  92. # Checking arg number
  93. check_args '1' "$#" 'user [format]'
  94. # Checking argument format
  95. format_validation 'user'
  96. # Checking user
  97. is_user_valid
  98. #----------------------------------------------------------#
  99. # Action #
  100. #----------------------------------------------------------#
  101. # Defining fileds to select
  102. fields='$IP $OWNER $STATUS $NAME'
  103. # Listing ips
  104. case $format in
  105. json) json_list_user_ips ;;
  106. plain) nohead=1; shell_list_user_ips ;;
  107. shell) shell_list_user_ips | column -t ;;
  108. *) check_args '1' '0' 'user [format]' ;;
  109. esac
  110. #----------------------------------------------------------#
  111. # Vesta #
  112. #----------------------------------------------------------#
  113. exit