v_list_sys_user_ips 3.5 KB

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