python编程实例-收集主机信息

收集主机信息:

主机名

ip

操作系统版本osver

服务器厂商vendor

服务器型号:product

服务器序列号:sn

CPU型号:cpu_model

CPU核数:cpu_num

内存大小:Memory

  1 #!/usr/bin/env python
  2 
  3 from subprocess import Popen,PIPE
  4 
  5 def getIfconfig():
  6     p = Popen(['ifconfig'],stdout=PIPE)
  7     data = p.stdout.read()
  8     return data
  9 
 10 def getDmi():
 11     p = Popen(['dmidecode'],stdout=PIPE)
 12     data = p.stdout.read()
 13     return data
 14 
 15 def parseData(data):
 16     parsed_data = []
 17     new_line = ''
 18     data = [i for i in data.split('
') if i]
 19     for line in data:
 20         if line[0].strip():
 21              parsed_data.append(new_line)
 22             new_line = line +'
'
 23         else:        
 24             new_line += line+'
'
 25     parsed_data.append(new_line)
 26     return [i for i in parsed_data if i]
 27 
 28 def parseIfconfig(parsed_data):
 29     parsed_data = [i for i in parsed_data if not i.startswith('lo')]
 30     for lines in parsed_data:
 31         line_list = lines.split('
')
 32         devname = line_list[0].split()[0]
 33         macaddr = line_list[0].split()[-1]
 34         ipaddr = line_list[1].split()[1].split(':')[1]
 35         break
 36     dic['ip'] =ipaddr
 37     return dic
 38 
 39 def parseDmi(parsed_data):
 40     dic = {}
 41     parsed_data = [i for i in parsed_data if i.startswith('System Information')]
 42     parsed_data = [i for i in parsed_data[0].split('
')[1:] if i]
 43     dmi_dic =  dict([i.strip().split(':') for i in parsed_data])
 44     dic['vendor'] = dmi_dic['Manufacturer']
 45     return dmi_dic
 46 
 47 def getHostname(f):
 48     with open(f) as fd:
 49         for line in fd:
 50             if line.startswith('HOSTNAME'):
 51                 hostname = line.split('=')[1].strip()
 52                 break
 53     return {'hostname':hostname}
 54 
 55 def getOsver(f):
 56     with open(f) as fd:
 57         for line in fd:
 58             osver = line.split()
 59             break
 60     return {'osver':osver}
 61 
 62 def getCpu(f):
 63     num = 0
 64     with open(f) as fd:
 65         for line in fd:
 66             if line.startswith('processor'):
 67                 num +=1
 68             if line.startswith('model name'):
 69                 cpu_model = line.split(':')[1].split()
 70                 cpu_model = cpu_model[0]+' '+cpu_model[-1]
 71     return {'cpu_num':num,'cpu_model':cpu_model}
 72 
 73 def getMemory(f):
 74     with open(f) as fd:
 75         for line in fd:
 76             if line.startswith('MemTotal'):
 77                 mem = int(line.split()[1].strip())
 78                 break
 79     mem = "%d" % int(mem/1024.0)+'M'
 80     return {'Memory':mem}
 81                 
 82 
 83 if __name__ == '__main__':
 84     dic = {}
 85     data_ip = getIfconfig()
 86     parsed_data_ip = parseData(data_ip)
 87     ip = parseIfconfig(parsed_data_ip)
 88     data_dmi = getDmi()
 89     parsed_data_dmi = parseData(data_dmi)
 90     dmi = parseDmi(parsed_data_dmi)
 91     hostname = getHostname('/etc/sysconfig/network')
 92     osver = getOsver('/etc/issue')
 93     cpu = getCpu('/proc/cpuinfo')
 94     mem = getMemory('/proc/meminfo')
 95     dic.update(ip)
 96     dic.update(dmi)
 97     dic.update(hostname)
 98     dic.update(osver)
 99     dic.update(cpu)
100     dic.update(mem)
101     print dic
原文地址:https://www.cnblogs.com/Nyan-Workflow-FC/p/5688385.html