linux下邮件发送服务器日志

linux下邮件发送服务器日志sendsyslog.py //发送邮件调用程序#!/usr/bin/envpython#-*-coding:UTF-8-*-importosimportsyssys.path.append(os.getcwd())importsendlog############sendlog.py//发送邮件配置程序#

linux下邮件发送服务器日志sendsyslog.py 

//发送邮件调用程序

#!/usr/bin/env python

# -*- coding: UTF-8 -*-

import os

import sys

sys.path.append(os.getcwd())

import sendlog

############

sendlog.py

//发送邮件配置程序

#!/usr/bin/env python

# -*- coding: UTF-8 -*-

”’

Created on 2012-7-25

@author: devops@qq.com

………..log……..

…..fcntl …….linux

………………pyc..,……py………

1…pyc .. python -c “import py_compile;py_compile.compile(r’/root/zyy/scripts/sendlog.py’)”

2…..py.. sendsyslog.py

#!/usr/bin/env python

# -*- coding: UTF-8 -*-

import os

import sys

sys.path.append(os.getcwd())

import sendlog

”’

from email.Header import Header

from email.MIMEText import MIMEText

from email.MIMEMultipart import MIMEMultipart

import smtplib

import datetime

import socket 

import fcntl

import struct 

 

def get_ip_address(ifname): 

    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) 

    return socket.inet_ntoa(fcntl.ioctl( 

        s.fileno(), 

        0x8915,  # SIOCGIFADDR 

        struct.pack(‘256s’, ifname[:15]) 

    )[20:24])

 

host = “host” + get_ip_address(‘eth0’).split(“.”)[3]

mail_user = “发件人”

mail_pass = “发件人邮箱密码”

mail_sender = “发件人邮箱”

mail_recipients = [ “收件人邮箱地址1”, “收件人邮箱地址2” ]

mail_server = “邮件服务器”

logpath = “发送附件存放目录”

logname = str(datetime.date.today()) + “.log”

logfile = logpath + logname

#……….

msg = MIMEMultipart()

#….

att = MIMEText(open(logfile, ‘rb’).read(), ‘base64’, ‘utf-8’)

att[“Content-Type”] = ‘application/octet-stream’

att[“Content-Disposition”] = ‘attachment; filename=%s %s’ % (host,logname)

msg.attach(att)

#….

msg[‘from’] = mail_sender

msg[‘subject’] = Header(‘Server %s daily log (‘ % host + str(datetime.date.today()) + ‘)’, ‘utf-8’)

#….

server = smtplib.SMTP()

server.connect(mail_server)

server.login(mail_user, mail_pass)

server.sendmail(mail_sender, mail_recipients, msg.as_string())

server.close()

“”今天的文章linux下邮件发送服务器日志分享到此就结束了,感谢您的阅读,如果确实帮到您,您可以动动手指转发给其他人。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/33812.html

(0)
编程小号编程小号

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注