python邮件发送

import platform
import smtplib
from datetime import datetime
def check_feature(feature,string):
if feature in string.lower():
return True
else:
return False

def get_value_from_string(key,string):
value = “NONE”
for line in string.split(“\n”):
if key in line:
value = line.split(“:”)[1].strip()
return value
cpu_features = []
with open(‘/proc/cpuinfo’) as cpus:
cpu_data = cpus.read()
num_of_cpus = cpu_data.count(“processor”)
cpu_features.append(“Number of Processors: {0}”.format(num_of_cpus))

one_processor_data = cpu_data.split("processor")[1]
print(one_processor_data)
if check_feature('vmx', one_processor_data):
    cpu_features.append("cpu Virtualization: enabled")
if check_feature('cpu_meltdown', one_processor_data):
    cpu_features.append("Know bugs: cpu meltdown")

model_name = get_value_from_string("model name",one_processor_data)
cpu_features.append("model name: {0}".format(model_name))

cpu_mhz = get_value_from_string("cpu MHz",one_processor_data)
cpu_features.append("CPU MHz: {0}".format(cpu_mhz))

with open(‘/proc/meminfo’) as memory:
memory_data = memory.read()
total_memory = get_value_from_string(“MemTotal”,memory_data).replace(‘kB’,“”)
free_memory = get_value_from_string(“MemFree”,memory_data).replace(“kB”,“”)
swap_memory = get_value_from_string(“SwapTotal”,memory_data).replace(“kB”,“”)
total_memory_in_gb = “Total Memory in GB:{0}”.format(int(total_memory)/1024)
free_memory_in_gb = “Free Memory in GB:{0}”.format(int(free_memory) / 1024)
swap_memory_in_gb = “Swap Memory in GB:{0}”.format(int(swap_memory) / 1024)
memory_features = [total_memory_in_gb,free_memory_in_gb,swap_memory_in_gb]

Data_sent_in_Email = “”
Header = “”“From: PythonEnterpriseAutomationBot
To: To Administrator
Subject: Monitoring System Report”“”

Data_sent_in_Email += Header
time_now = datetime.now().strftime(“%Y-%m-%d %H:%M:%S”)
Data_sent_in_Email += “Time Now is {0}\n”.format(time_now)

Data_sent_in_Email += “\n===system information\n”
Data_sent_in_Email += “”"
system type: {0}
hostname: {1}
kernal version: {2}
system version: {3}
machine architeture: {4}
python verson: {5}
“”".format(platform.system(),
platform.uname()[1],
platform.uname()[2],
platform.version(),
platform.machine(),
platform.python_version())

Data_sent_in_Email += “\n===cpu information=\n”
Data_sent_in_Email += “\n”.join(cpu_features)

Data_sent_in_Email += “\nmemory information===\n”
Data_sent_in_Email += “\n”.join(memory_features)

fromaddr = ‘xxxxxxx.com.cn’
toaddr = ‘xxxxxxx.com.cn’
username = ‘xxxxxxx.com.cn’
password = ‘xxxxxxx’

server = smtplib.SMTP(‘smtp.office365.com:587’)
server.ehlo()
server.starttls()
server.login(username, password)
print(Data_sent_in_Email)
server.sendmail(fromaddr, toaddr, Data_sent_in_Email)
server.quit()

  • 14
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值