| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- #!/bin/bash
- # info: list user ips
- # options user [format]
- #
- # The function for obtainig the list of available ip addresses.
- #----------------------------------------------------------#
- # Variable&Function #
- #----------------------------------------------------------#
- # Argument defenition
- user=$1
- format=${2-shell}
- # Importing variables
- source $VESTA/conf/vars.conf
- source $V_FUNC/shared.func
- # Json function
- json_list_user_ips() {
- # Print top bracket
- echo '{'
- owned_ips=$(grep -l "OWNER='$user'" $V_IPS/*)
- shared_ips=$(grep -H -A5 "OWNER='vesta'" $V_IPS/* |\
- grep "STATUS='shared'"|\
- cut -f 1 -d - )
- ip_list="$owned_ips\n$shared_ips"
- fileds_count=$(echo "$fields" | wc -w)
- # Starting main loop
- for IP in $(echo -e "$ip_list" | sort -u); do
- IP=$(basename $IP)
- ip_data=$(cat $V_IPS/$IP)
- # Assing key=value
- eval $ip_data
- # Closing bracket if there already was output
- if [ -n "$data" ]; then
- echo -e ' },'
- fi
- i=1
- for field in $fields; do
- eval value=$field
- if [ $i -eq 1 ]; then
- # Printing parrent
- (( ++i))
- echo -e "\t\"$value\": {"
- else
- # Printing child
- if [ $i -lt $fileds_count ]; then
- (( ++i))
- echo -e "\t\t\"${field//$/}\": \"${value//,/, }\","
- else
- echo -e "\t\t\"${field//$/}\": \"${value//,/, }\""
- data=1
- fi
- fi
- done
- done
- # Closing bracket if there was output
- if [ -n "$data" ]; then
- echo -e ' }'
- fi
- # Printing bottom bracket
- echo -e '}'
- }
- # Shell function
- shell_list_user_ips() {
- owned_ips=$(grep -l "OWNER='$user'" $V_IPS/*)
- shared_ips=$(grep -H -A5 "OWNER='vesta'" $V_IPS/* |\
- grep "STATUS='shared'"|\
- cut -f 1 -d - )
- ip_list="$owned_ips\n$shared_ips"
- if [ -z "$nohead" ]; then
- # Print brief info
- echo "${fields//$/}"
- for a in $fields; do
- echo -e "--------- \c"
- done
- echo
- fi
- # Starting main loop
- for IP in $(echo -e "$ip_list" | sort -u); do
- IP=$(basename $IP)
- ip_data=$(cat $V_IPS/$IP)
- # Assign key/value config
- eval $ip_data
- # Print result line
- eval echo "$fields"
- done
- }
- #----------------------------------------------------------#
- # Verifications #
- #----------------------------------------------------------#
- # Checking arg number
- check_args '1' "$#" 'user [format]'
- # Checking argument format
- format_validation 'user'
- # Checking user
- is_user_valid
- #----------------------------------------------------------#
- # Action #
- #----------------------------------------------------------#
- # Defining fileds to select
- fields='$IP $OWNER $STATUS $NAME'
- # Listing ips
- case $format in
- json) json_list_user_ips ;;
- plain) nohead=1; shell_list_user_ips ;;
- shell) shell_list_user_ips | column -t ;;
- *) check_args '1' '0' 'user [format]' ;;
- esac
- #----------------------------------------------------------#
- # Vesta #
- #----------------------------------------------------------#
- exit
|