用Python收集Linux系统信息

本文最后更新于2014年6月22日,已超过 1 年没有更新,如果文章内容失效,还请反馈给我,谢谢!

通过读取Linux系统中的一些关键性文件以及调用相关命令来查看系统信息:

#!/usr/bin/env python
#coding=utf-8
import os
mem=[]
net=[]

def dump(content):
    for id, item in enumerate(content, 1):
        print("%d:" % id)
        for k in item:
            print(" %s: %s" % (k, item[k]))
    print("\n")

def parse(content):
    info = {}
    for line in content:
        t = line.split(": ")
        if len(t) < 2:
            continue
        name = t[0].strip()
        value = t[1].strip()
        info[name] = value
    return info

data = os.popen("/usr/sbin/dmidecode").read()
sectors = data.split("\n\n")
for sector in sectors:
    lines = sector.split("\n")
    if len(lines) < 3:
        continue
    title = lines[1]
    info = parse(lines)
    if title == 'Memory Device':
        if not info['Size'].startswith('No'):
            mem.append({'size':info['Size']})

data = os.popen("/sbin/ip -o -f inet addr").read()
lines = data.split("\n")
for line in lines:
    items = line.split()
    if len(items) < 4:
        continue
    net.append({'name':items[1],'addr':items[3]})


print("[OS]")
os.system('/bin/uname -nsr')
print(open('/etc/issue.net').readline())

print("[CPU]")
lines=open('/proc/cpuinfo').readlines()
info=parse(lines)
print("Model Name: %s\nAddress sizes: %s\n" % (info['model name'], info['address sizes']))
os.system('/usr/bin/lscpu')
print("\n")

print("[Memory]")
lines=open('/proc/meminfo').readlines()
info=parse(lines)
print("total: %s ; swap: %s" % (info['MemTotal'], info['SwapTotal']))
dump(mem)

print("[FS]")
os.system('/bin/lsblk')
print("")
os.system('/bin/df -h')

print("\n")
print("[Net]")
dump(net)

=EOF=

声明: 除非注明,ixyzero.com文章均为原创,转载请以链接形式标明本文地址,谢谢!
https://ixyzero.com/blog/archives/41.html

《用Python收集Linux系统信息》上的2个想法

  1. Linux的内部渗透测试工具,可用于通过SMB枚举OS信息,域信息,共享,目录和用户 (Internal penetration testing tool for Linux that can be used to enumerate OS information, domain information, shares, directories, and users through SMB.)
    https://github.com/m8r0wn/nullinux

发表评论

邮箱地址不会被公开。 必填项已用*标注