MOTDfetch/motdfetch.d/99-userslog
2023-02-07 16:10:46 +07:00

78 lines
2.7 KiB
Bash

#!/usr/bin/env bash
#
# check last user logged in (with host ip and period) and currently logged in users
# + if the last logged in is not the current user, add a section about the last login of the curent user
# GENERAL ###########################################################
# locale env
unset LC_ALL
export LC_MESSAGES=C
# check if module was disabled
module_disable=${module_userslog_disable:=0}
if (($module_disable == 1)); then
exit 1
fi
# colors
c_txt=${c_txt:="39"}
c_txt_emphase=${c_txt_emphase:="35"}
c_txt_deco=${c_txt_deco:="97"}
c_txt_invert=${c_txt_invert:="30"}
c_bg=${c_bg_sec:="47"}
c_danger=${c_danger:="31"}
c_warning=${c_warning:="33"}
c_success=${c_success:="32"}
c_title=${c_title:="${c_bg};1;${c_txt_invert}m"}
# PREPARATIONS ######################################################
lastuserlog=$(last -i -F -n 2 | head -2 | tail -1)
lastusername=$(echo -e "$lastuserlog" | awk '{print $1}')
lastuserhost=$(echo -e "$lastuserlog" | awk '{print $3}')
if [[ "$lastuserhost" == "0.0.0.0" ]]; then
lastuserhost="localhost"
fi
lastusertime=$(echo -e "$lastuserlog" | awk '{$1=$2=$3=""; print $0}' | sed 's/^ //')
lastcurrentuserlog=$(last $(id -un) -i -F -n 2 | head -2 | tail -1)
lastcurrentusername=$(echo -e "$lastcurrentuserlog" | awk '{print $1}')
lastcurrentuserhost=$(echo -e "$lastcurrentuserlog" | awk '{print $3}')
if [[ "$lastcurrentuserhost" == "0.0.0.0" ]]; then
lastcurrentuserhost="localhost"
fi
lastcurrentusertime=$(echo -e "$lastcurrentuserlog" | awk '{$1=$2=$3=""; print $0}' | sed 's/^ //')
loggedusers=$(last -i -F -p now | /usr/bin/grep "logged in" | awk '{print $1}')
loggedusersnames=$(echo -e "$loggedusers" | uniq -c | sed 's/^\s\+/|/' | tr ' ' '|')
loggeduserscount=$(echo -e "$loggedusers" | wc -l)
# OUTPUT ############################################################
# header
echo -e "\n\e[${c_title} Users log \e[0m\n"
W="\e[0;${c_txt}m"
X="\e[1;${c_txt}m"
F="\e[0;${c_txt_emphase}m"
G="\e[1;${c_txt_emphase}m"
S="\e[1;4;${c_txt}m"
N="\e[0m"
# last logins
if [[ "$lastusername" == "$lastcurrentusername" ]]; then
echo -e "$X Last user logged in$W: $G$lastcurrentusername$W, from $F$lastcurrentuserhost$W
$X | Login - out$W (duration): $F$lastcurrentusertime$N"
else
echo -e "$X Last user logged in$W: $G$lastusername$W, from $F$lastuserhost$W
$X | Login - out$W (duration): $F$lastusertime$W
$X Last time$W $G$lastcurrentusername$W$X logged in$W, was from $F$lastcurrentuserhost$W
$X | Login - out$W (duration): $F$lastcurrentusertime$N"
fi
# logged users
echo
echo -e "$G"$loggeduserscount"$W ${X}session$W(s)$X are logged in$W:|${S}Session(s)${W}|${S}User$W${G}\n`echo -e "$loggedusersnames"`$N" | column -t -s '|' | sed 's/^/ /'