Files
VestaCP/bin/v_list_user_ips
2012-02-20 12:46:25 +02:00

135 lines
3.7 KiB
Bash
Executable File

#!/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 '{'
fileds_count=$(echo "$fields" | wc -w)
for IP in $(ls $V_IPS/); do
source $V_IPS/$IP
if [ "$OWNER" = "$user" ]; then
eval echo "$fields"
# 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
else
if [ "$OWNER" = 'admin' ] && [ "$STATUS" = 'shared' ]; then
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
fi
fi
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() {
for IP in $(ls $V_IPS/); do
source $V_IPS/$IP
if [ "$OWNER" = "$user" ]; then
eval echo "$fields"
else
if [ "$OWNER" = 'admin' ] && [ "$STATUS" = 'shared' ]; then
eval echo "$fields"
fi
fi
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