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

相關文章
相關標籤/搜索