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

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

 

转载于:https://my.oschina.net/u/2313186/blog/853488

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值