linux 脚本-账号检测发邮件脚本

第一初始化脚本

主要完成发送邮件的软件安装和邮件的配置初始化

#!/bin/bash

yum install -y mail* sendmail* postfix*
service sendmail start

cp /etc/mail.rc /etc/mail.rc.bak
cat > /etc/mail.rc<<EOF
set from=warning@xxx.com     #登录邮箱的地址
set smtp=smtp.xxxx.com   #登录发送邮箱的服务器地址
set smtp-auth-user=xxxxx    #登录邮箱的用户
set smtp-auth-password=xxxx   #登录邮箱用户的密码
set smtp-auth=login
EOF

第二启动脚本

#!/bin/bash

iface=eth1  #所在机器的网卡名
while true;do
info=`w|grep <username>`
if [ ! -n "$info" ]; then
ip=$(ip addr show "$iface"|grep "inet"|awk '{print $2}')
#ip=${ip%/*}
echo "$(date) ${ip} logout" | mail -v -s "login warning" <发送的邮件地址>
sleep 2 
nohup sh  test.sh > /tmp/account/log.txt 2>&1 &
kill -9 $$
fi
sleep 2 
done

  

第三检测脚本

while true;do
info=`w|grep <username>`
iface=eth1
if [ -n "$info" ]; then
 ip=$(ip addr show "$iface"|grep "inet"|awk '{print $2}')
 echo "$(date) $ip login" | mail -v -s "logout warning!!!" <发送的邮件地址>  
 sleep 2
 nohup sh  start.sh > /tmp/account/log.txt 2>&1 &
 kill -9 $$ 
fi 

done

  

原文地址:https://www.cnblogs.com/kuku0223/p/12758621.html