
如何從 Linux (Ubuntu) 的使用者清單中 grep 所有登入的使用者?
到目前為止我必須:
cat /etc/passwd | grep "/home" | cut -d: -f1
答案1
正如上面評論的 w 或 who 向您顯示誰已登入..
您也可以查看歷史記錄/目前數據,查看lastlog
和lastb 表示最近一次錯誤登入。
w|grep pts|awk '{print $1}'
for ids in $(w|grep pts|awk '{print $1"_"$2}'); do id=${ids%%_*}; pts=${ids##*_}; actualperson=$(getent passwd $id|awk '{print $5}'); echo "Username: $id is $actualperson and is logged into $pts"; done;
使用者名稱: xxx 已登入 pts/0
使用者名稱: xxx 已登入 pts/5
for ids in $(w|grep pts|awk '{print $1"_"$2}'); do
id=${ids%%_*};
pts=${ids##*_};
actualperson=$(getent passwd $id|awk '{print $5}');
# echo to your console the persons details
echo "Username: $id is $actualperson and is logged into $pts";
# Send a message to person logged in telling them you know their logged in
echo "I know your logged in $actualperson"|tee /dev/$pts 2>&1>/dev/null;
done;
答案2
該users
命令可能會有幫助。從手冊頁:
users - print the user names of users currently logged in to the current host