用python获取服务器硬件信息[转]

 1 #!/usr/bin/env  python
 2 # -*- coding: utf-8 -*-
 3 import rlcompleter, readline
 4 readline.parse_and_bind('tab: complete')
 5 import dmidecode
 6 import   time
 7 import   os
 8 import   re
 9 system=dmidecode.system()
10 print "\033[1;36;40m%s\033[0m" %"获取服务器硬件信息"
11 for x,y in system.items():
12 for i in y['data'].items():
13 if  i[0] == 'Product Name':
14 print "\033[1;31;40m%s\033[0m" % "-"*10
15 print 'Server models: %s'  %i[1]   
16 print i
17  
18 print "\033[1;36;40m%s\033[0m" % "获取服务器CPU信息"
19 for x,y  in dmidecode.processor().items():
20     for m,n in y.items():
21     if m=='data':
22 print "\033[1;31;40m%s\033[0m" % "-"*10
23     for x,y in n.items():
24         print x,y
25 print "\033[1;36;40m%s\033[0m" %"获取服务器BIOS信息"
26 for x,y in dmidecode.bios().items():
27 for m,n in y['data'].items():
28     if m.find('Characteristic')!=-1:
29         for x,y in n.items():
30         print "\033[1;34;40m%s\033[0m" % "-"*10
31         print x,y
32     else:
33         print "\033[1;32;40m%s\033[0m" % "-"*10
34         print m,n
35 print "\033[1;36;40m%s\033[0m" %"获取服务器内存信息"
36 for x,y in dmidecode.memory().items():
37     for m,n in y['data'].items():
38         print "\033[1;34;40m%s\033[0m" % "-"*10
39         print m,n
40 #便于调试,可以删除
41 print "x"*50
42 print "\033[1;36;40m%s\033[0m" %"获取服务器主板信息"
43 for x,y in dmidecode.baseboard().items():
44     #print  x,y
45     for m,n in y['data'].items():
46         print "\033[1;34;40m%s\033[0m" % "-"*10
47         print m,n
48 print "\033[1;36;40m%s\033[0m" %"获取服务器主板插槽信息"
49 for x,y in dmidecode.slot().items():
50     for m,n in y['data'].items():
51         print "\033[1;34;40m%s\033[0m" % "-"*10
52         print m,n
53 print "\033[1;36;40m%s\033[0m" %"获取服务器网卡信息"
54 """
55 安装linux硬件信息收集工具包
56    """
57 #os.system('yum  -y install  make  wget  gcc* ;wget http://ezix.org/software/files/lshw-B.02.14.tar.gz ; tar -zxvf  lshw-B.02.14
58 .tar.gz ;cd lshw-B.02.14 ; make &&  make install ; cd .. ; rm -rf lshw-B.02.14* ')
59 netcard=os.popen('lshw -C network   ').read( )
60 print "产品名称: %s"         % re.findall('product:.+(?#测试)',netcard)[0]
61 print "网卡速度: %s"         % re.findall('size:.+(?#测试)',netcard)[0]
62 n=len(re.findall('\*-network:',netcard))
63 if n==0:
64     print  "网卡IP地址: %s"          %re.findall('ip=\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}',netcard)[0] 
65 else:
66     for i  in range(0,n):
67          try:
68             print  "网卡IP地址: %s" %re.findall('ip=\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}',netcard)[i]
69         except IndexError:
70             i+=1
71             print "第%s块网卡没有IP地址" %i 
72 #print "网卡IP地址: %s"       % re.findall('ip=\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}',netcard)[0]
73 print "网卡状态: %s "        % re.findall('link=\w{3}(?#测试)',netcard)[0]
74 print "网卡MAC地址: %s "     % re.findall('serial:.+(?#测试)',netcard)[0]
75 print "网卡厂家: %s "        % re.findall('vendor:.+(?#测试)',netcard)[0]
76 print "网络接口名称: %s"     % re.findall('logical name:.+(?#测试)',netcard)[0]
77 """
78 获取系统信息
79 """
80 print "\033[1;36;40m%s\033[0m" %"获取服务器操作系统信息"
81 import platform
82 print "系统cpu位数: %s "  % platform.processor()
83 print "系统信息: %s "     % platform.system()
84 print "操作系统类型: %s"  % platform.dist()[0]
85 print "系统主机名: %s "   % platform.node()
86 
87                                                                                         

 

posted @ 2017-05-11 08:22  zhangjpn  阅读(5630)  评论(0编辑  收藏  举报