
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
コマンドはおそらく役に立つでしょう。man ページから:
users - print the user names of users currently logged in to the current host