用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                                                                                         

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM