用python收集系統資訊

2022-05-30 23:27:10 字數 4225 閱讀 7220

執行結果類似如下:

key: value

product: vmware virtual platform

modelname: intel(r) xeon(r) cpu e5-2620 v3 @ 2.40ghz

totalmem: 1.5 gb

cpucore: 2

vender: vmware, inc.

hostname: archlinux.vsphere

distroname: arch linux

sn: vmware-56 4d b9 19 26 63 ad ae-41 f3 5c 3c 34 66 ec bd

#!/usr/bin/env python

# -*- encoding: utf-8 -*-

''' 可以在多linux平台下的多種機器下工作,需要新增相應的鍵值對兒在os_type字典裡面 '''

from subprocess import popen, pipe

# 定義作業系統主機名字段字典, 在獲取不同型別主機名配置檔案的時候使用

os_type =

def parsecfg(osname):

' 通過不同的主機名,返回對應的主機名的配置檔案位置 '

for type in os_type:

if osname.startswith( os_type[type][0] ):

cfg = os_type[ type ][ 1 ]

return cfg

def gethostinfo(command):

''' @args: command 輸入要執行的系統命令

@return: 返回乙個長的字串

'''p = popen([command], stdout=pipe)

data = p.stdout.read()

return data

def parsehostinfo(data):

newline = '\n'

parseddata =

eachsection = ''

''' regenerate a list, remove the null element

@args: data is a long string

@return: all of sections list

'''data = [i for i in data.split('\n') if i]

for eachline in data:

if eachline[0].strip():

eachsection = eachline + newline

else:

eachsection = eachsection + eachline + newline

return [i for i in parseddata if i]

def parseipinfo(parseddata):

dic = {}

data = [i for i in parseddata if i and not i.startswith('lo')]

for lines in data:

linelst = lines.split('\n')

devname = linelst[0].split(':')[0]

ipaddr = linelst[1].split()[1]

macaddr = linelst[3].split()[1]

dic[devname] = [ipaddr, macaddr]

return dic

def parsedmiinfo(parseddata):

''':param parseddata:

:return:

'''dmi_result = {}

data = [i for i in parseddata if i.startswith('system information')]

data = [i for i in data[0].split('\n')[1:] if i]

'生成乙個大列表,每個元素也是乙個列表,且僅有兩個元素,所以下面可以直接生成乙個字典資料結構'

l_list = [i.strip().split(':') for i in data]

sys_info_dic = dict(l_list)

dmi_result['vender'] = sys_info_dic['manufacturer'].strip()

dmi_result['product'] = sys_info_dic['product name'].strip()

dmi_result['sn'] = sys_info_dic['serial number'].strip()

return dmi_result

def getdistroname():

with open('/etc/issue') as fd:

distroname = ' '.join(fd.read().strip().split()[:2])

return

def gethostname(fn, osver):

with open(fn) as fd:

host = fd.read()

if osver.startswith('arch linux') or osver.startswith('ubuntu'):

hostname = host.strip()

return

if osver == 'centos release':

host = host.strip().split('\n')

for i in host:

if i.startswith('hostname'):

hostname = i.split('=')[1]

return

def getmeminfo():

with open('/proc/meminfo') as fd:

for eachline in fd:

if eachline.startswith('memtotal'):

totalmem = eachline.split(':')[1].strip()

totalmem = '%.1f' % (float(totalmem.split()[0]) / 1024.0 / 1024)

return

def getcpuinfo():

with open('/proc/cpuinfo') as fd:

for eachline in fd:

if eachline.startswith('cpu cores'):

cpucore = eachline.split(':')[1].strip()

continue

if eachline.startswith('model name'):

modelname = eachline.split(':')[1].strip()

return

if __name__ == '__main__':

dic = {}

cfg = '/etc/sysconfig/network'

data = gethostinfo('dmidecode')

dmilst = parsehostinfo(data)

' dmi info '

dmiinfo = parsedmiinfo(dmilst)

meminfo = getmeminfo()

osver = getdistroname()

osname = osver['distroname']

cfg = parsecfg(osname)

hostinfo = gethostname(cfg, osname)

cpuinfo = getcpuinfo()

dic.update(dmiinfo)

dic.update(hostinfo)

dic.update(meminfo)

dic.update(osver)

dic.update(cpuinfo)

print('%10s: %s' % ('key', 'value'))

for key in dic.items():

print('%10s: %s' % (key[0], key[1]))

Python 收集系統資訊

收集主機的以下資訊,並以字典形式輸出。1 主機名 hostname 3 作業系統版本 osver 4 伺服器廠商 vendor 5 伺服器型號 product 6 伺服器序列號 sn 7 cpu型號 cpu model 8 cpu核數 cpu num 9 記憶體大小 memory 如下 vim co...

python收集 Python收集主機資訊

python收集linux主機資訊,需要安裝dmidecode命令,yum y install dmidecode usr bin env python coding utf 8 from subprocess import popen,pipe 獲取ifconfig命令資訊 def getifco...

資訊收集篇 玩轉資訊收集(一)

都知道,資訊收集這個東西在各行各業都能用到,在偵探業,現場的勘察以及細節資訊需要了解 it 網路安全 黑客這方面也更是如此,要談資訊收集這個東西說起來覆蓋的業界可謂是非常的廣泛,今天我就主要是在計算機行業這一塊做一些簡要的說明,以至於一些朋友也就不會連資訊收集是個什麼毛東西都不知道。資訊收集 inf...