1.安装mailx
yum install mailx -y
2.配置证书
mkdir -p /root/.certs echo -n | openssl s_client -connect smtp.qq.com:465 | sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' > ~/.certs/qq.crt certutil -A -n "GeoTrust SSL CA" -t "C,," -d ~/.certs -i ~/.certs/qq.crt certutil -A -n "GeoTrust Global CA" -t "C,," -d ~/.certs -i ~/.certs/qq.crt certutil -L -d /root/.certs cd /root/.certs certutil -A -n "GeoTrust SSL CA - G3" -t "Pu,Pu,Pu" -d ./ -i qq.crt
3.修改mailx配置文件
vim /etc/mail.rc
在文件末尾添加:
set bsdcompat set sendcharsets=iso-8859-1,utf-8 set [email protected] set smtp=smtps://smtp.qq.com:465 set [email protected] set smtp-auth-password=ABC123 set smtp-auth=login set ssl-verify=ignore set nss-config-dir=/root/.certs
PS:这里配的是QQ邮箱,其中smtp-auth-password是授权码不是密码。
怎么拿授权码:
4.发送邮件测试
echo "this is a prisoner break "|mail -s "test" [email protected]
5.实践:
本机安装了denghosts,为了能看到相关的统计信息,每天发一封邮件到我的邮箱
[root@teddylu1987 .certs]# list=$(sed -n /^sshd/p /etc/hosts.deny) [root@teddylu1987 .certs]# list2=$(sed -n /^sshd/p /etc/hosts.deny|wc -l) [root@teddylu1987 .certs]# list3=$(cat /etc/hosts.deny) [root@teddylu1987 .certs]# echo -e "$list"|mail -s "linode-denyhosts-status" [email protected] [root@teddylu1987 .certs]# echo -e "$list2"|mail -s "linode-denyhosts-status" [email protected] [root@teddylu1987 .certs]# echo -e "$list3"|mail -s "linode-denyhosts-status" [email protected] [root@teddylu1987 shell]# cat denyhosts.sh #!/bin/bash RETVAL=0 FAILCOUNT=0 SCRIPT_PATH="/home/crontab" MAIL_GROUP="[email protected] [email protected] [email protected]" list1=$(sed -n /^sshd/p /etc/hosts.deny) list2=$(sed -n /^sshd/p /etc/hosts.deny|wc -l) list3=$(cat /etc/hosts.deny) echo -e "$list1"|mail -s "linode-denyhosts-status-about-denyhosts" ${MAIL_GROUP} echo -e "$list2"|mail -s "linode-denyhosts-status-about-numbers" ${MAIL_GROUP} crontab -l #check denyhosts status everynight 9:30pm 30 21 * * * /bin/sh /backup/shell/denyhosts.sh >/dev/null 2 >&1
centos7安装mailx