master:
#/usr/bin/python
# -*- coding: UTF-8 -*-
import socket
#獲取主機名
hostname = socket.gethostname()
#獲取ip地址
ipadd = socket.gethostbyname(socket.getfqdn(socket.gethostname()))
import time
#獲取當前系統(tǒng)時間
localtime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
HOST = "smtp.163.com"
SUBJECT = "[Warning] keepalived by test "
TO = "morb@x.x.x.x.com.cn"
FROM = "x.x.x.x.x@163.com"
text = "時間:%s\nIP:%s\n主機名:%s\n事件:change to Master, VIP: 192.168.1.130" % (localtime, ipadd, hostname)
BODY = string.join((
"From: %s" %FROM,
"TO: %s" %TO,
"Subject: %s" %SUBJECT,
"",
text
),"\r\n")
server = smtplib.SMTP()
server.connect(HOST,"25")
server.login("x.x.x.x@163.com","授權(quán)碼")
server.sendmail(FROM,[TO],BODY)
server.quit()
backup
#/usr/bin/python
# -*- coding: UTF-8 -*-
import smtplib
import string
import socket
#獲取主機名
hostname = socket.gethostname()
#獲取ip地址
ipadd = socket.gethostbyname(socket.getfqdn(socket.gethostname()))
import time
#獲取當前系統(tǒng)時間
localtime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
HOST = "smtp.163.com"
SUBJECT = "[Warning] keepalived by test "
TO = "morb@x.x.x.x.x.com.cn"
FROM = "x.x.x.x.@163.com"
text = "時間:%s\nIP:%s\n主機名:%s\n事件:change to Backup, VIP: 192.168.1.130" % (localtime, ipadd, hostname)
BODY = string.join((
"From: %s" %FROM,
"TO: %s" %TO,
"Subject: %s" %SUBJECT,
"",
text
),"\r\n")
server = smtplib.SMTP()
server.connect(HOST,"25")
server.login("xxxxx@163.com","授權(quán)碼")
server.sendmail(FROM,[TO],BODY)
server.quit()