linux shell监控登陆的脚本

linux shell 记录每个用户的登陆情况,结合crontab定期的发送给管理员
 
#!/bin/bash
username=`whoami`
time=`date +%y-%m-%d\ %H:%M:%S`
terminal=`ps | grep bash | grep -v grep | awk '{print $2}'`
location=`w | grep "$terminal" | awk '{print $3}'`
if [[ "$location" =~ "-" ]] || [[ "$location" =~ ^":" ]]
then
        location='localhost'
echo -e "$username\t$location\t$time" >> /var/log/login.info
 
crontab -e
* 16 * * */5   mail -s "user info " [email protected] < /var/log/login.info

 

你可能感兴趣的:(linux,shell,shell)