zoukankan      html  css  js  c++  java
  • 使用 python 收集获取 Linux 系统主机信息

    使用 python 代码收集主机的系统信息,主要:主机名称、IP、系统版本、服务器厂商、型号、序列号、CPU信息、内存等系统信息。

    #!/usr/bin/env python
    #encoding: utf-8
    
    '''
    收集主机的信息:
    主机名称、IP、系统版本、服务器厂商、型号、序列号、CPU信息、内存信息
    '''
    
    from subprocess import Popen, PIPE
    import os,sys
    
    ''' 获取 ifconfig 命令的输出 '''
    def getIfconfig():
        p = Popen(['ifconfig'], stdout = PIPE)
        data = p.stdout.read()
        return data
    
    ''' 获取 dmidecode 命令的输出 '''
    def getDmi():
        p = Popen(['dmidecode'], stdout = PIPE)
        data = p.stdout.read()
        return data
    
    ''' 根据空行分段落 返回段落列表'''
    def parseData(data):
        parsed_data = []
        new_line = ''
        data = [i for i in data.split('
    ') if i]
        for line in data:
            if line[0].strip():
                parsed_data.append(new_line)
                new_line = line + '
    '
            else:
                new_line += line + '
    '
        parsed_data.append(new_line)
        return [i for i in parsed_data if i]
    
    ''' 根据输入的段落数据分析出ifconfig的每个网卡ip信息 '''
    def parseIfconfig(parsed_data):
        dic = {}
        parsed_data = [i for i in parsed_data if not i.startswith('lo')]
        for lines in parsed_data:
            line_list = lines.split('
    ')
            devname = line_list[0].split()[0]
            macaddr = line_list[0].split()[-1]
            ipaddr  = line_list[1].split()[1].split(':')[1]
            break
        dic['ip'] = ipaddr
        return dic
    
    ''' 根据输入的dmi段落数据 分析出指定参数 '''
    def parseDmi(parsed_data):
        dic = {}
        parsed_data = [i for i in parsed_data if i.startswith('System Information')]
        parsed_data = [i for i in parsed_data[0].split('
    ')[1:] if i]
        dmi_dic = dict([i.strip().split(':') for i in parsed_data])
        dic['vender'] = dmi_dic['Manufacturer'].strip()
        dic['product'] = dmi_dic['Product Name'].strip()
        dic['sn'] = dmi_dic['Serial Number'].strip()
        return dic
    
    ''' 获取Linux系统主机名称 '''
    def getHostname():
        with open('/etc/sysconfig/network') as fd:
            for line in fd:
                if line.startswith('HOSTNAME'):
                    hostname = line.split('=')[1].strip()
                    break
        return {'hostname':hostname}
    
    ''' 获取Linux系统的版本信息 '''
    def getOsVersion():
        with open('/etc/issue') as fd:
            for line in fd:
                osver = line.strip()
                break
        return {'osver':osver}
    
    ''' 获取CPU的型号和CPU的核心数 '''
    def getCpu():
        num = 0
        with open('/proc/cpuinfo') as fd:
            for line in fd:
                if line.startswith('processor'):
                    num += 1
                if line.startswith('model name'):
                    cpu_model = line.split(':')[1].strip().split()
                    cpu_model = cpu_model[0] + ' ' + cpu_model[2]  + ' ' + cpu_model[-1]
        return {'cpu_num':num, 'cpu_model':cpu_model}
    
    ''' 获取Linux系统的总物理内存 '''
    def getMemory():
        with open('/proc/meminfo') as fd:
            for line in fd:
                if line.startswith('MemTotal'):
                    mem = int(line.split()[1].strip())
                    break
        mem = '%.f' % (mem / 1024.0) + ' MB'
        return {'Memory':mem}
    
    if __name__ == '__main__':
        dic = {}
        data_ip = getIfconfig()
        parsed_data_ip = parseData(data_ip)
        ip = parseIfconfig(parsed_data_ip)
        
        data_dmi = getDmi()
        parsed_data_dmi = parseData(data_dmi)
        dmi = parseDmi(parsed_data_dmi)
    
        hostname = getHostname()
        osver = getOsVersion()
        cpu = getCpu()
        mem = getMemory()
        
        dic.update(ip)
        dic.update(dmi)
        dic.update(hostname)
        dic.update(osver)
        dic.update(cpu)
        dic.update(mem)
    
        ''' 将获取到的所有数据信息并按简单格式对齐显示 '''
        for k,v in dic.items():
            print '%-10s:%s' % (k, v)
    
     

    实验测试结果:

    product   :VMware Virtual Platform
    osver     :CentOS release 6.4 (Final)
    sn        :VMware-56 4d b4 6c 05 e5 20 dc-c6 49 0c e1 e0 18 1c 75
    Memory    :1870 MB
    cpu_num   :2
    ip        :192.168.0.8
    vender    :VMware, Inc.
    hostname  :vip
    cpu_model :Intel(R) i7-4710MQ 2.50GHz
  • 相关阅读:
    【NXOpen.UF扩展】修改表达式
    NX二次开发 克隆
    C++手动加载CLR运行托管程序(CLR Hosting)
    C++/CLR 使用(VS2012,VS2013,VS2015)编写
    解决VS2015安装后stdio.h ucrtd.lib等文件无法识别问题,即include+lib环境变量配置
    NX 图标
    【错误分析】NX error status: 32
    NX CAM 读取加工参数
    CAM 模板样式表
    锁定NX原生界面 & 锁定界面更新
  • 原文地址:https://www.cnblogs.com/liwei0526vip/p/6172275.html
Copyright © 2011-2022 走看看