import xlsxwriter import json,os,sys,math import argparse import paramiko
workbook = xlsxwriter.Workbook('disk.xlsx') worksheet = workbook.add_worksheet("系统磁盘统计")
ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
def BatchCMD(address,username,password,port,command): try: ssh.connect(hostname=address,username=username,password=password,port=port,timeout=2) stdin , stdout , stderr = ssh.exec_command(command) result = stdout.read() if len(result) != 0: result = str(result).replace("\\n", "\n") result = result.replace("b'", "").replace("'", "") return result else: return None except Exception: return None
def GetAllDiskSpace(address,username,password,port): ref = [] cmd_dict = {"Linux\n" : "df | grep -v 'Filesystem' | awk '{print $6 \":\" $3 \":\" $4 \":\" $5}'", "AIX\n" : "df | grep -v 'Filesystem' | awk '{print $4 \":\" $7}'" } os_version = BatchCMD(address,username,password,port,"uname") for version,run_cmd in cmd_dict.items(): if(version == os_version): os_ref = BatchCMD(address,username,password,port,run_cmd) ref_list= os_ref.split("\n") for each in ref_list: if each != "": ref_list = [] ref_list = str(each.split(":")) ref.append(eval(ref_list)) return ref
def CreateDiskTable(address,data,section): merge_format = workbook.add_format( {'bold': True, 'border': 1, 'align': 'center', 'valign': 'vcenter', 'fg_color': '#EEAEEE'})
header_count = len(data[1]) merge_format1 = workbook.add_format( {'bold': True, 'border': 1, 'align': 'center', 'valign': 'vcenter', 'fg_color': '#AEEEEE'})
header_range = "A{}:B{}".format(section,section+header_count) worksheet.write_row(header_range, data[0], merge_format1)
header_merge_range = "A{}:B{}".format(section,section) worksheet.merge_range(header_merge_range, "IP地址", merge_format1)
address_merge_range = "A{}:B{}".format(section+1,section+header_count) worksheet.merge_range(address_merge_range, address , merge_format)
merge_format2 = workbook.add_format( {'bold': True, 'border': 1, 'valign': 'vcenter', 'fg_color': '#D7E4BC', 'align': 'center'})
index_range = "C{}".format(section+1) worksheet.write_column(index_range, data[1], merge_format2) index_range = "C{}:C{}".format(section+1,section+1) worksheet.set_column(index_range, 30)
merge_format3 = workbook.add_format( {'bold': True, 'border': 1, 'valign': 'vcenter', 'fg_color': '#D7E4BC', 'align': 'center'})
index_range = "D{}".format(section + 1) worksheet.write_column(index_range, data[2], merge_format3)
index_range = "D{}:D{}".format(section + 1, section + 1) worksheet.set_column(index_range, 20)
merge_format4 = workbook.add_format( {'bold': True, 'border': 1, 'valign': 'vcenter', 'fg_color': '#D7E4BC', 'align': 'center'}) index_range = "E{}".format(section + 1) worksheet.write_column(index_range, data[3], merge_format4) index_range = "E{}:E{}".format(section + 1, section + 1) worksheet.set_column(index_range, 20)
merge_format5 = workbook.add_format( {'bold': True, 'border': 1, 'valign': 'vcenter', 'fg_color': '#D7E4BC', 'align': 'center'})
index_range = "F{}".format(section + 1) worksheet.write_column(index_range, data[4], merge_format5) index_range = "F{}:F{}".format(section + 1, section + 1) worksheet.set_column(index_range, 20)
return section + header_count + 3
if __name__ == "__main__":
''' val = \ [ ["IP地址", "IP地址", "磁盘路径", "总容量", "剩余容量", "利用率"], ["/etc/system/winsss/aaa", "/proc/", "/sys", "/abc/lyshark"], ["1024GG", "2048GB", "111GB", "1111GB"], ["1024GG", "2048GB", "111GB", "22GB"], ["10%", "50%", "20%", "33%"] ] ref = CreateDiskTable("192.168.1.1",val,3) ref = CreateDiskTable("192.168.1.2", val, ref) CreateDiskTable("192.168.1.3", val, ref) '''
ref = GetAllDiskSpace("192.168.191.4","root","1233",22)
ref_xor = list ( map(list,zip(*ref)) )
header = ["IP地址", "IP地址", "磁盘路径", "已使用", "剩余容量", "利用率"]
ref_xor.insert(0,header) print(ref_xor)
CreateDiskTable("192.168.191.4", ref_xor, 2) workbook.close()
|