myvesta/bin/v-list-user-log
2023-06-04 17:56:44 +02:00

115 lines
2.8 KiB
Bash
Executable File

#!/bin/bash
# info: list user log
# options: USER [FORMAT]
#
# The function of obtaining the list of 100 last users commands.
#----------------------------------------------------------#
# Variable&Function #
#----------------------------------------------------------#
# Argument definition
user=$1
format=${2-shell}
limit=${3-300}
# Includes
source $VESTA/func/main.sh
# JSON list function
json_list() {
IFS=$'\n'
i=1
objects=$(echo "$logs" |wc -l)
echo "{"
for str in $logs; do
ID=$(echo "$str" |cut -f 2 -d \')
DATE=$(echo "$str" |cut -f 4 -d \')
TIME=$(echo "$str" |cut -f 6 -d \')
CMD=$(echo "$str" |cut -f 8 -d \')
CMD=${CMD//\"/\\\"}
echo -n ' "'$ID'": {
"CMD": "'$CMD'",
"UNDO": "'$UNDO'",
"TIME": "'$TIME'",
"DATE": "'$DATE'"
}'
if [ "$limit" -gt 0 ] && [ "$i" = "$limit" ]; then
break;
fi
if [ "$i" -lt "$objects" ]; then
echo ','
else
echo
fi
((i++))
done
echo '}'
}
shell_list() {
IFS=$'\n'
echo "DATE~TIME~CMD"
echo "----~----~---"
for str in $logs; do
DATE=$(echo "$str" |cut -f 4 -d \')
TIME=$(echo "$str" |cut -f 6 -d \')
CMD=$(echo "$str" |cut -f 8 -d \')
echo "$DATE~$TIME~$CMD"
done
}
# PLAIN list function
plain_list() {
IFS=$'\n'
for str in $logs; do
DATE=$(echo "$str" |cut -f 4 -d \')
TIME=$(echo "$str" |cut -f 6 -d \')
CMD=$(echo "$str" |cut -f 8 -d \')
echo -e "$ID\t$CMD\t$UNDO\t$TIME\t$DATE"
done
}
# CSV list function
csv_list() {
IFS=$'\n'
echo "ID,CMD,UNDO,TIME,DATE"
for str in $logs; do
DATE=$(echo "$str" |cut -f 4 -d \')
TIME=$(echo "$str" |cut -f 6 -d \')
CMD=$(echo "$str" |cut -f 8 -d \')
echo "$ID,\"$CMD\",\"$UNDO\",$TIME,$DATE"
done
}
#----------------------------------------------------------#
# Verifications #
#----------------------------------------------------------#
check_args '1' "$#" 'USER [FORMAT] [LIMIT]'
is_format_valid 'user'
is_object_valid 'user' 'USER' "$user"
#----------------------------------------------------------#
# Action #
#----------------------------------------------------------#
# Parsing history log
logs=$(tail -n $limit $USER_DATA/history.log | tac)
case $format in
json) json_list ;;
plain) plain_list ;;
csv) csv_list ;;
shell) shell_list |column -t -s '~';;
esac
#----------------------------------------------------------#
# Vesta #
#----------------------------------------------------------#
exit