wanmin444 发表于 2018-8-12 13:56:38

Python收集主机信息

#!/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 =
  
    for line in data:
  
      if line.strip():
  
            parsed_data.append(new_line)
  
            new_line = line + '\n'
  
      else:
  
            new_line += line + '\n'
  
    parsed_data.append(new_line)
  
    return
  

  
#处理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.split()
  
      macaddr = line_list.split()
  
      ipaddr = line_list.split()
  
      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 = .split('\n') if i]
  
    dmi_dic = dict()
  
    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('=').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(':').split()
  
                cpu_model = cpu_model+' '+ cpu_model+' '+ 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().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 =
  
#    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()
页: [1]
查看完整版本: Python收集主机信息