如何从 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