How does python get server hardware information
- 2020-06-01 10:08:57
- OfStack
In this paper, we share the specific code of Android9 grid picture for your reference. The specific content is as follows
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import rlcompleter, readline
readline.parse_and_bind('tab: complete')
import dmidecode
import time
import os
import re
system=dmidecode.system()
print "\033[1;36;40m%s\033[0m" %" Get the server hardware information "
for x,y in system.items():
for i in y['data'].items():
if i[0] == 'Product Name':
print "\033[1;31;40m%s\033[0m" % "-"*10
print 'Server models: %s' %i[1]
print i
print "\033[1;36;40m%s\033[0m" % " Fetch server CPU information "
for x,y in dmidecode.processor().items():
for m,n in y.items():
if m=='data':
print "\033[1;31;40m%s\033[0m" % "-"*10
for x,y in n.items():
print x,y
print "\033[1;36;40m%s\033[0m" %" Fetch server BIOS information "
for x,y in dmidecode.bios().items():
for m,n in y['data'].items():
if m.find('Characteristic')!=-1:
for x,y in n.items():
print "\033[1;34;40m%s\033[0m" % "-"*10
print x,y
else:
print "\033[1;32;40m%s\033[0m" % "-"*10
print m,n
print "\033[1;36;40m%s\033[0m" %" Gets server memory information "
for x,y in dmidecode.memory().items():
for m,n in y['data'].items():
print "\033[1;34;40m%s\033[0m" % "-"*10
print m,n
# Easy to debug, can be deleted
print "x"*50
print "\033[1;36;40m%s\033[0m" %" Get the server motherboard information "
for x,y in dmidecode.baseboard().items():
#print x,y
for m,n in y['data'].items():
print "\033[1;34;40m%s\033[0m" % "-"*10
print m,n
print "\033[1;36;40m%s\033[0m" %" Get server motherboard slot information "
for x,y in dmidecode.slot().items():
for m,n in y['data'].items():
print "\033[1;34;40m%s\033[0m" % "-"*10
print m,n
print "\033[1;36;40m%s\033[0m" %" Get server network card information "
"""
The installation linux Hardware information collection kit
"""
#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
.tar.gz ;cd lshw-B.02.14 ; make && make install ; cd .. ; rm -rf lshw-B.02.14* ')
netcard=os.popen('lshw -C network ').read( )
print " The product name : %s" % re.findall('product:.+(?# test )',netcard)[0]
print " Network speed : %s" % re.findall('size:.+(?# test )',netcard)[0]
n=len(re.findall('\*-network:',netcard))
if n==0:
print " The network card IP address : %s" %re.findall('ip=\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}',netcard)[0]
else:
for i in range(0,n):
try:
print " The network card IP address : %s" %re.findall('ip=\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}',netcard)[i]
except IndexError:
i+=1
print " The first %s Block network card no IP address " %i
#print " The network card IP address : %s" % re.findall('ip=\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}',netcard)[0]
print " Nic status : %s " % re.findall('link=\w{3}(?# test )',netcard)[0]
print " The network card MAC address : %s " % re.findall('serial:.+(?# test )',netcard)[0]
print " Network card manufacturer : %s " % re.findall('vendor:.+(?# test )',netcard)[0]
print " Network interface name : %s" % re.findall('logical name:.+(?# test )',netcard)[0]
"""
Get system information
"""
print "\033[1;36;40m%s\033[0m" %" Gets server operating system information "
import platform
print " system cpu digits : %s " % platform.processor()
print " System information : %s " % platform.system()
print " Operating system type : %s" % platform.dist()[0]
print " System host name : %s " % platform.node()