4324234 发表于 2017-1-9 16:37:44

Python收集主机信息

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#!/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收集主机信息