您的位置:首页 > 编程语言 > Python开发

通过python的paramiko抓取多台服务器信息,并通过html格式发送邮件到群组

2017-03-07 00:00 609 查看
1, Paramiko安装依赖于pycrypto、ecdsa模块

#!/bin/env python

import paramiko
import sys
import smtplib
import string
import time
from email.mime.text import MIMEText
from email.header import Header
from email.mime.multipart import *
from email.utils import COMMASPACE,formatdate
def GetNowTime(s):
return time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(s))

class Df_infor(object):

def __init__(self,host,mount,total,used,free):
self.hostname = host
self.mount_point = mount
self.total_size = total
self.used_size = used
self.free_percent = free

information = []
#Filesystem Type Size Used Avail Use% Mounted on
#masters = [] ''' /msdp'''
#medias = [] ''' /media '''
ip_list = ['127.0.0.1','10.8.36.61']
df = Df_infor('hostname','mount_point','total_size','used_size','free_percent')
information.append(df)

for ip in ip_list:
host = ip
#host=sys.argv[1]
#cmd_master = 'df -Th /msdp'
#cmd_media = 'df -Th /media'i
cmd = " df -ThP | awk -F' ' '{print $1, $2, $3, $4, $5, $6, $7}' "
hostname = 'hostname'
#cmd = 'ls'
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())

#ssh.connect(host,username='root',password='Opsa@8))Best>com',allow_agent=True)
ssh.connect(host,username='root',password='800best',allow_agent=True)
stdin,stdout,stderr = ssh.exec_command(hostname)
hostname = stdout.read().split('\n')[0]
stdin,stdout,stderr = ssh.exec_command(cmd)
results = stdout.readlines()

for line in results:
line = line.split('\n')[0]
line_list = line.split(' ')
if 'T' in line_list[2]:
size = int(line_list[2].split('T')[0])
if size >= 10:
free = 100-int(line_list[5].split('%')[0])
free_percent = str(free)+'%'
df = Df_infor(hostname,line_list[6],line_list[2],line_list[3],free_percent)
information.append(df)
else:
pass
else:
continue

for infor in information:
print infor.hostname+ ' ' + infor.mount_point + ' ' + infor.total_size + ' ' + infor.used_size + ' ' + infor.free_percent + '%'

html_head = "<table border='1'> <caption style='background:#7FFFD4' >Disk used information</caption>" + " <tr style='background:#FAEBD7'>" + \
" <td>hostname</td>" + \
" <td>mount_point</td>" + \
" <td>total_size</td>" + \
" <td>used_size</td>" + \
" <td>free_percent</td>" + \
"</tr> "
html_body = ""
for infor in information:
if infor.hostname == 'hostname':
continue
else:
html_body = html_body + "<tr style='background:#C0C0C0'>" + '<td align="center">' + infor.hostname + '</td>' + \
'<td align="center" >' + infor.mount_point +'</td>' + \
'<td align="center">' + infor.total_size + '</td>' + \
'<td align="center">' + infor.used_size + '</td>' + \
'<td align="center">' + infor.free_percent + '</td>' + \
'</tr>'

html_tail = '</table>'

html_str = html_head + html_body + html_tail
print html_str

now_time = GetNowTime(time.time())
HOST = "server_host"
subject = 'Netbackup daily report' + '-' + now_time
TO = ['adress1@qq.com','adress2@qq.com']
FROM='nbuadmin@800best.com'
msgtext = MIMEText(html_str,'html','utf-8')
msg = MIMEMultipart('related')
msg['From'] = FROM
msg['To'] = ','.join(TO)
msg['Subject'] = subject
msg['Date'] = formatdate(localtime=True)
msg.attach(msgtext)
server = smtplib.SMTP()
#server = smtplib.SMTP_SSL(HOST)
server.connect(HOST,'25')
server.sendmail(FROM,TO,msg.as_string())
server.quit()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: