前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python收集主机信息

Python收集主机信息

作者头像
py3study
发布2020-01-08 18:52:03
1.3K0
发布2020-01-08 18:52:03
举报
文章被收录于专栏:python3

Python收集linux主机信息,需要安装dmidecode命令,yum -y install dmidecode

代码语言:javascript
复制
#!/usr/bin/env python
# coding=utf-8
 
from subprocess import Popen, PIPE
 
#获取ifconfig命令信息
def getIfconfig():
    p = Popen(['ifconfig'], stdout=PIPE)
    data = p.stdout.read().decode()
    return data
 
#获取dmidecode命令信息
def getDmi():
    p = Popen(['dmidecode'], stdout=PIPE)
    data = p.stdout.read().decode()
    return data
 
#处理data数据输出为list并去掉空
def parseData(data):
    parsed_data = []
    new_line = ''
    data = [i for i in data.split('\n') if i]
    for line in data:
        if line[0].strip():
            parsed_data.append(new_line)
            new_line = line + '\n'
        else:
            new_line += line + '\n'
    parsed_data.append(new_line)
    return [i for i in parsed_data if i]
 
#处理ifconfig数据并获取eth1的IP地址
def parseIfocnfig(parsed_data):
    dic = {}
    parsed_data = [ i for i in parsed_data if i.startswith('eth1')]
    for lines in parsed_data:
        line_list = lines.split('\n')
        devname = line_list[0].split()[0]
        macaddr = line_list[2].split()[1]
        ipaddr = line_list[1].split()[1]
        break
    dic['ip'] = ipaddr
    return dic
 
#处理dmidecode数据获取信息
def paeseDmi(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('\n')[1:] if i]
    dmi_dic = dict([i.strip().split(': ') for i in parsed_data])
    dic['vender'] = dmi_dic['Manufacturer']
    dic['product'] = dmi_dic['Product Name']
    dic['sn'] = dmi_dic['Serial Number'][:8]
    return dic
 
#获取hostname
def getHostname(f):
    with open(f) as fd:
        for line in fd:
            if line.startswith('HOSTNAME'):
                hostname = line.split('=')[1].strip()
                break
    return {'hostname': hostname}
 
#获取操作系统
def getOSver(f):
    with open(f) as fd:
        for line in fd:
            osver = line.strip()
            break
    return {'osver': osver}
 
#获取CPU数
def getCpu(f):
    num = 0
    with open(f) as fd:
        for line in fd:
            if line.startswith('processor'):
                num += 1
            if line.startswith('model name'):
                cpu_model = line.split(':')[1].split()
                cpu_model = cpu_model[0]+' '+ cpu_model[3]+' '+ cpu_model[-1]
    return {'cpu_num': num, 'cpu_model': cpu_model}
 
#获取内存大小
def getMemory(f):
    with open(f) as fd:
        for line in fd:
            if line.startswith('MemTotal'):
                mem = int(line.split()[1].strip())
                break
    mem = "%s" % int((mem/1024.0))+'M'
    return {'memory': mem}
 
def main():
    dic = {}
    ip = parseIfocnfig(parseData(getIfconfig()))
    dmi = paeseDmi(parseData(getDmi()))
    osver = getOSver('/etc/issue')
    hostname = getHostname('/etc/sysconfig/network')
    cpu = getCpu('/proc/cpuinfo')
    memory = getMemory('/proc/meminfo')
#    lines = [ip, dmi, hostname, osver, cpu, memory]
#    for i in lines:
#        dic.update(i)
    dic.update(ip)
    dic.update(dmi)
    dic.update(hostname)
    dic.update(osver)
    dic.update(cpu)
    dic.update(memory)
    print(dic)
 
if __name__ == '__main__':
    main()
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019/08/31 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档