MailReader项目:
- 通过
python
读取并解析每日指定邮件内容- 通过
SSH
隧道连接指定MySQL
服务器- 将解析数据保存至
MySQL
指定表- 使用
Launchctl
开启定时任务- GitHub: https://github.com/zhoujl48/WY_MailReader/
文章目录
1 项目代码解析
- 配置文件
# Server Config
LOCAL_HOST = '127.0.0.1'
SSH_HOST = '***'
SSH_PORT = 32200
SSH_PKEY = '***'
SSH_USER = '***'
SSH_PASSWORD = '***'
# MySQL Config
MYSQL_HOST = '***'
MYSQL_PORT = 3306
MYSQL_USER = '***'
MYSQL_PASSWORD = '***'
MYSQL_DB = '***'
# 查询关键字
KEY_WORD_SUBJECT = '***'
# 账户密码
MAIL_ACCOUNT = '***'
MAIL_PASSWORD = '***'
IMAP_HOST = '***'
IMAP_PORT = 993
1.1 数据库
1.1.1 MySQL配置
- 创建新表
-- 新建表
create table if not exists anti_plugin.nsh_ids_scripts (
`id` int(11) primary key not null auto_increment comment '主键id',
`role_id` bigint(20) comment 'role_id',
`ts_start` datetime comment '日志开始时间',
`ts_end` datetime comment '日志结束时间',
`last_modify_time` timestamp default CURRENT_TIMESTAMP not null on update CURRENT_TIMESTAMP comment 'timestamp'
) engine=InnoDB default charset=utf8
- MySQL数据字段示例
1.1.2 MySQL类
# python3连接MySQL包
import pymysql
# Query to insert row into database nsh_evaluate
QUERY_SQL_INSERT = """
INSERT INTO anti_plugin.nsh_ids_scripts(role_id, ts_start, ts_end)
VALUES ({role_id}, '{ts_start}', '{ts_end}')
"""
# MySQL类
class MysqlDB(object):
def __init__(self, host, port, user, passwd, db):
self._conn = pymysql.connect(host=host, port=port, user=user