CentOS 7.7使用Shell脚本监控系统登陆用户数并配置QQ邮箱发送邮件告警

1、配置QQ邮箱发送邮件:vim

# yum -y install mailxbash

# cp /etc/mail.rc{,.bak}ide

# vim /etc/mail.rc,末尾新增以下代码:测试

set from=834143808@qq.comspa

set smtp=smtp.qq.com3d

set smtp-auth-user=834143808@qq.comblog

set smtp-auth-password=QQ邮箱受权码crontab

set smtp-auth=loginip

# echo "Hello World" | mail -s "test" 834143808@qq.comit

image.png


2、编写Shell脚本监控系统登陆用户数:

目标:系统登陆用户数大于3人,实现邮件告警

image.png

# mkdir -pv /scripts

# vim /scripts/check_users.sh

#!/bin/bash

users=`who | wc -l`

if [ $users -gt 3 ];then

echo "当前系统登陆用户数为$users" | mail -s "system login users" 834143808@qq.com

fi

# chmod +x /scripts/check_users.sh


3、编写任务计划:

# crontab -e

*/5 * * * * /scripts/check_users.sh &> /dev/null


4、多开链接窗口,模拟多用户登陆,测试邮件告警:

image.png

image.png

image.png

相关文章
相关标签/搜索