v_list_user_ips 3.5 KB

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