shell脚本检查用户登录

时间:2020-03-05 15:31:46  来源:igfitidea点击:

在本教程中,我们向我们展示shell脚本来检查用户登录和注销。
此shell脚本发送电子邮件,其中包含有关登录和注销服务器上的每个用户的信息。

这有助于Sysadmin意识到用户登录和注销服务器以获取监视目的。

shell脚本检查用户登录

此脚本输出每个用户登录和注销时间并发送电子邮件。
该脚本正在Linux中使用Last命令获取结果。

#!/bin/bash
subject="New login on $(hostname -f)"
email="[email protected]"
while true; do
LASTLOGIN=`last | head -1`
if [ x"$PREVLOGIN" = "x" ]; then
PREVLOGIN=$LASTLOGIN
else
if [ "$LASTLOGIN" != "$PREVLOGIN" ];
then
#echo "$LASTLOGIN" | mail -s "$subject" "$email"
echo "$LASTLOGIN $subject"
PREVLOGIN=$LASTLOGIN
fi
fi
sleep 1
done

脚本输出

bobbin@theitroad:/$./lastlogin.sh
bobbin pts/4 :0.0 Tue Nov 26 15:54 still logged in New login on theitroad.lviv.example.com
bobbin pts/4 :0.0 Tue Nov 26 15:54 - 15:54 (00:00) New login on theitroad.lviv.example.com
bobbin pts/4 :0.0 Tue Nov 26 15:54 still logged in New login on theitroad.lviv.example.com
bobbin pts/4 :0.0 Tue Nov 26 15:54 - 15:54 (00:00) New login on theitroad.lviv.example.com
^C

按"Ctrl + C"退出脚本。