Py发送邮件测试脚本
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import smtplib
from email.mime.text import MIMEText
from email.header import Header
sender = 'username@163.net'
receivers = ['123456@qq.com'] # 接收邮件,可设置为你的QQ邮箱或者其他邮箱
# 三个参数:第一个为文本内容,第二个 plain 设置文本格式,第三个 utf-8 设置编码
message = MIMEText('Python 邮件发送测试...', 'plain', 'utf-8')
message['From'] = Header("菜鸟教程", 'utf-8') # 发送者
message['To'] = Header("测试", 'utf-8') # 接收者
subject = 'Python SMTP 邮件测试'
message['Subject'] = Header(subject, 'utf-8')
try:
server = smtplib.SMTP()
server.connect('smtp.163.net') #连接服务器
server.login('username', 'password') #登录操作
server.sendmail(sender, receivers, message.as_string())
server.close()
print("email send success.")
except Exception as e:
print("email send failed: " , str(e))
上面这个脚本发送次数多了可能会有限制,可以尝试用下面的脚本:
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import smtplib
from email.mime.text import MIMEText
from email.header import Header
from email.mime.multipart import MIMEMultipart
import time
import os
mail_host="smtp.163.net"
mail_user="***@163.net"
mail_pass="***********"
sslPort="465"
time1=time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))
sender = '***@163.net'
to_reciver = ['*****@163.net']
cc_reciver =[ '123456@qq.com']
reciver = to_reciver + cc_reciver
message = MIMEText('请管理员尽快处理!!!', _subtype='html', _charset='utf-8')
message['From'] = sender
message['To'] = ";".join(to_reciver)
message['Cc'] = ";".join(cc_reciver)
subject ='接口自动化报告'+'-'+time1
message['Subject'] = Header(subject, 'utf-8')
try:
smtpObj = smtplib.SMTP_SSL(mail_host,sslPort)
smtpObj.ehlo()
smtpObj.login(mail_user,mail_pass)
smtpObj.sendmail(sender,reciver, message.as_string())
print ("邮件发送成功")
except Exception as n:
print ("Error: 无法发送邮件")
print(n)
exit
修改邮箱配置代码:
update_email.sh
#!/bin/sh
#echo >update_email.sh && vim update_email.sh
#bash update_email.sh
#配置蜜罐告警邮件收发
echo "############ 是否配置Opencanary_Web蜜罐邮件收发,输入yes/no?Enter.默认no. ############"
typeset -l select
read select
case $select in
y*)
get_mail_host=`sed -n '30p' /usr/local/src/opencanary_web/application.py |cut -d ' ' -f3`
#30p application.py的第30行正好是配置smtp服务器地址的那行
get_mail_user=`sed -n '31p' /usr/local/src/opencanary_web/application.py |cut -d ' ' -f3`
get_mail_pass=`sed -n '32p' /usr/local/src/opencanary_web/application.py |cut -d ' ' -f3`
get_mail_postfix=`sed -n '33p' /usr/local/src/opencanary_web/application.py |cut -d ' ' -f3`
echo "############正在配置opencanary蜜罐邮件收发###########"
read -p "smtp服务器地址($get_mail_host):" mail_host
if [ "$mail_host" = "" ]; then
echo "$get_mail_host"
else
sed -i "s/$get_mail_host/\"$mail_host\"/g" /usr/local/src/opencanary_web/application.py
fi
read -p "邮箱用户名($get_mail_user):" mail_user
if [ "$mail_user" = "" ]; then
echo "$get_mail_user"
else
sed -i "s/$get_mail_user/\"$mail_user\"/g" /usr/local/src/opencanary_web/application.py
fi
read -p "输入邮箱密码($get_mail_pass):" mail_pass
if [ "$mail_pass" = "" ]; then
echo "$get_mail_pass"
else
sed -i "s/$get_mail_pass/\"$mail_pass\"/g" /usr/local/src/opencanary_web/application.py
fi
read -p "邮箱后缀名($get_mail_postfix):" mail_postfix
if [ "$mail_postfix" = "" ]; then
echo "$get_mail_postfix"
else
sed -i "s/$get_mail_postfix/\"$mail_postfix\"/g" /usr/local/src/opencanary_web/application.py
fi
echo "############配置已完成,下一步配置收件人邮箱###########"
get_mail_addressee=`sed -n '2p' /usr/local/src/opencanary_web/util/conf/email.ini | awk '{print $3}'`
read -p "收件人邮箱($get_mail_addressee):" mail_addressee
if [ "$mail_addressee" = "" ]; then
echo "########配置没有做任何更改,默认收件人邮箱:$get_mail_addressee#######"
else
sed -i "s/$get_mail_addressee/$mail_addressee/g" /usr/local/src/opencanary_web/util/conf/email.ini
get_new_mail_addressee=`sed -n '2p' /usr/local/src/opencanary_web/util/conf/email.ini | awk '{print $3}'`
echo "##########已更新告警收件邮箱:$get_new_mail_addressee#########"
fi
mail_switch=`sed -n '3p' /usr/local/src/opencanary_web/util/conf/email.ini |awk '{print $3}'`
if [ "$mail_switch" = "on" ]; then
echo "#######已开启告警邮件开关########"
else
echo "#######正在开启告警邮件开关##########"
sed -i "s/switch = off/switch = on/g" /usr/local/src/opencanary_web/util/conf/email.ini
echo "#######开启告警邮件成功##########"
fi
echo "############告警邮件配置已完成############"
echo "############正在重启服务############"
sleep 5
#重启服务配置生效
systemctl restart supervisord.service
systemctl restart nginx.service
#回显已完成
echo "已经配置成功蜜罐告警邮件,具体配置浏览/usr/local/src/opencanary_web/application.py"
echo "收件人邮件配置(以及告警开关):/usr/local/src/opencanary_web/util/conf/email.ini"
echo "更多信息请参考https://github.com/p1r06u3/opencanary_web"
;;
n*)
echo "蜜罐告警邮件没有配置成功,请自行决定是否需要配置."
echo "蜜罐告警具体配置(发件人)浏览/usr/local/src/opencanary_web/application.py"
echo "收件人邮件配置(以及告警开关):/usr/local/src/opencanary_web/util/conf/email.ini"
echo "更多信息请参考https://github.com/p1r06u3/opencanary_web"
;;
*)
echo "蜜罐告警邮件没有配置成功,请自行决定是否需要配置."
echo "蜜罐告警具体配置(发件人)浏览/usr/local/src/opencanary_web/application.py"
echo "收件人邮件配置(以及告警开关):/usr/local/src/opencanary_web/util/conf/email.ini"
echo "更多信息请参考https://github.com/p1r06u3/opencanary_web"
esac
exit 0
vim /root/.opencanary.conf
{
"device.node_id": "opencanary-1",
"server.ip": "172.18.214.121",
"device.listen_addr": "172.18.214.120",
"host.enabled": true,
"git.enabled": true,
"git.port" : 9418,
"ftp.enabled": true,
"ftp.port": 21,
"ftp.banner": "FTP server ready",
"http.banner": "Apache/2.2.22 (Ubuntu)",
"http.enabled": true,
"http.port": 80,
"http.skin": "nasLogin",
"http.skin.list": [
{
"desc": "Plain HTML Login",
"name": "basicLogin"
},
{
"desc": "Synology NAS Login",
"name": "nasLogin"
}
],
"httpproxy.enabled" : true,
"httpproxy.port": 8080,
"httpproxy.skin": "squid",
"httproxy.skin.list": [
{
"desc": "Squid",
"name": "squid"
},
{
"desc": "Microsoft ISA Server Web Proxy",
"name": "ms-isa"
}
],
"logger": {
"class": "PyLogger",
"kwargs": {
"formatters": {
"plain": {
"format": "%(message)s"
}
},
"handlers": {
"console": {
"class": "logging.StreamHandler",
"stream": "ext://sys.stdout"
},
"file": {
"class": "logging.FileHandler",
"filename": "/var/tmp/opencanary.log"
}
}
}
},
"portscan.enabled": true,
"portscan.logfile":"/var/log/kern.log",
"portscan.synrate": 5,
"portscan.nmaposrate": 5,
"portscan.lorate": 3,
"smb.auditfile": "/var/log/samba-audit.log",
"smb.enabled": false,
"mysql.enabled": true,
"mysql.port": 3306,
"mysql.banner": "5.5.43-0ubuntu0.14.04.1",
"ssh.enabled": true,
"ssh.port": 2222,
"ssh.version": "SSH-2.0-OpenSSH_5.1p1 Debian-4",
"redis.enabled": true,
"redis.port": 6379,
"rdp.enabled": false,
"rdp.port": 3389,
"sip.enabled": false,
"sip.port": 5060,
"snmp.enabled": false,
"snmp.port": 161,
"ntp.enabled": false,
"ntp.port": "123",
"tftp.enabled": true,
"tftp.port": 69,
"tcpbanner.maxnum":10,
"tcpbanner.enabled": false,
"tcpbanner_1.enabled": false,
"tcpbanner_1.port": 8001,
"tcpbanner_1.datareceivedbanner": "cmdserver",
"tcpbanner_1.initbanner": "console",
"tcpbanner_1.alertstring.enabled": false,
"tcpbanner_1.alertstring": "cmd",
"tcpbanner_1.keep_alive.enabled": false,
"tcpbanner_1.keep_alive_secret": "password",
"tcpbanner_1.keep_alive_probes": 11,
"tcpbanner_1.keep_alive_interval":300,
"tcpbanner_1.keep_alive_idle": 300,
"telnet.enabled": false,
"telnet.port": "23",
"telnet.banner": "TelnetServer",
"telnet.honeycreds": [
{
"username": "admin",
"password": "$pbkdf2-sha512$19000$bG1NaY3xvjdGyBlj7N37Xw$dGrmBqqWa1okTCpN3QEmeo9j5DuV2u1EuVFD8Di0GxNiM64To5O/Y66f7UASvnQr8.LCzqTm6awC8Kj/aGKvwA"
},
{
"username": "admin",
"password": "admin1"
}
],
"mssql.enabled": false,
"mssql.version": "2012",
"mssql.port":1433,
"vnc.enabled": false,
"vnc.port":5000
}
opencanaryd --restart