v_list_sys_user_ips 3.3 KB

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