linux shell 用戶統計 時間 用戶總數 近期活躍用戶 具有root權限的用戶 當前在線用戶 登錄ip tty

linux shell 用戶統計 時間 用戶總數 近期活躍用戶 具有root權限的用戶 當前在線用戶 登錄ip tty

Time=`date "+%Y-%m-%d__%H:%M:%S"`

eval $(awk -F: -v sum=0 '{if ($3 >= 1000 && $3 != 65534) {sum+=1; printf("All["sum"]=%s;", $1)}} \
END {printf("Sum=%d\n",sum)}' /etc/passwd)

Active=`last -w | cut -d " " -f 1 | grep -v wtmp | grep -v reboot | grep -v "^$" | sort | uniq -c | sort -k1 -n -r | awk -v num=3 '{if(num>0){printf(",%s", $2); num--}}' | cut -c 2-`

eval $(awk -F: '{if($3==1000){printf("User_1000=%s", $1)}}' /etc/passwd)

UserWithRoot=${User_1000}
Users=`cat /etc/group | grep sudo | cut -d : -f 4 | tr ',' ' '`

for i in ${Users};do
    if [[ $i == ${User_1000} ]] ;then
        continue
    fi
    UserWithRoot="${UserWithRoot},$i"
done

if [[ -r /etc/sudoers ]] ;then
    for i in ${All[*]};do
        grep -q "^${i}" /etc/sudoers
        if [[ $? -eq 0 ]] ;then
            UserWithRoot="${UserWithRoot},$i"
        fi
    done
fi

UserLogedIn=`w -h | awk '{printf(",%s_%s_%s", $1, $3, $2)}' | cut -c 2-`

echo "${Time} ${UserSum} [${Active}] [${UserWithRoot}] [${UserLogedIn}]"


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章