收集主機的以下資訊,並以字典形式輸出。
1、主機名:hostname
3、作業系統版本:osver
4、伺服器廠商:vendor
5、伺服器型號:product
6、伺服器序列號:sn
7、cpu型號:cpu_model
8、cpu核數:cpu_num
9、記憶體大小:memory
**如下:
vim collect_info.py
#!/usr/bin/env python
from subprocess import popen, pipe
def getifconfig():
p = popen(['ifconfig'], stdout=pipe)
data = p.stdout.read()
return data
def getdmi():
p = popen(['dmidecode'], stdout=pipe)
data = p.stdout.read()
return data
def parsedata(data):
parsed_data =
new_line = ''
data = [i for i in data.split('\n') if i]
for line in data:
if line[0].split():
new_line = line + '\n'
else:
new_line += line + '\n'
return [i for i in parsed_data if i and not i.startswith('lo')]
def parseifconfig(parsed_data):
dic = {}
for lines in parsed_data:
line_list = lines.split('\n')
devname = line_list[0].split()[0]
macaddr = line_list[0].split()[-1]
ipaddr = line_list[1].split()[1].split(':')[1]
break
dic['ip'] = ipaddr
return dic
def parsedmi(parsed_data):
dic = {}
parsed_data = [i for i in parsed_data if i.startswith('system information')]
parsed_data = [i for i in parsed_data[0].split('\n')[1:] if i]
dmi_dic = dict([i.strip().split(':') for i in parsed_data])
dic['vendor'] = dmi_dic['manufacturer'].strip()
dic['product'] = dmi_dic['product name'].strip()
dic['sn'] = dmi_dic['serial number'].strip()[:15]
return dic
def gethostname(f):
with open(f) as fd:
for line in fd:
if line.startswith('hostname'):
hostname = line.split('=')[1].strip()
break
return
def getosver(f):
with open(f) as fd:
for line in fd:
osver = line.strip()
break
return
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(':')[1].split()
cpu_model = cpu_model[0]+' '+cpu_model[-1]
return
def getmemory(f):
with open(f) as fd:
for line in fd:
if line.startswith('memtotal'):
mem = int(line.split()[1].strip())
break
mem = "%s" % int(mem/1024.0)+'m'
return
if __name__ == '__main__':
dic = {}
data_ip = getifconfig()
parsed_data_ip = parsedata(data_ip)
ip = parseifconfig(parsed_data_ip)
data_dmi = getdmi()
parsed_data_dmi = parsedata(data_dmi)
dmi = parsedmi(parsed_data_dmi)
hostname = gethostname('/etc/sysconfig/network')
osver = getosver('/etc/issue')
cpu = getcpu('/proc/cpuinfo')
mem = getmemory('/proc/meminfo')
dic.update(ip)
dic.update(dmi)
dic.update(hostname)
dic.update(osver)
dic.update(cpu)
dic.update(mem)
print dic
驗證結果如下:
用python收集系統資訊
執行結果類似如下 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.hostnam...
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...