linux下郵件發送伺服器日誌

來源:互聯網
上載者:User

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()


""

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.