在本文中,我们将会探索使用Python编程语言工具来检索Linux系统各种信息。走你。
哪个Python版本?
当我提及Python,所指的就是CPython 2(准确的是2.7).我会显式提醒那些相同的代码在CPython 3 (3.3)上是不工作的,以及提供一份解释不同之处的备选代码。请确保你已经安装了CPython,在终端上输入python或者python3回车,然后你在终端上应该能看到python的提示符(prompt)。
请注意,所有的程序在它们第一行都是#!/usr/bin/env/python,也就是说,我们想要Python的解释器来执行这些脚本。因此,如果你想你的脚本具有执行性,请使用chmod +x your-script.py, 那么你就可以使用./your-script.py来执行它了(在本文中你将会看到这种方式)
探索platform模块
platform模块在标准库中,它有很多运行我们获得众多系统信息的函数。让我们运行Python解释器来探索它们中的一些函数,那就从platform.uname()函数开始吧:
>>> import platform
>>> platform.uname()
('Linux', 'fedora.echorand', '3.7.4-204.fc18.x86_64', '#1 SMP Wed Jan 23 16:44:29 UTC 2013', 'x86_64')
</div>
如果你已知道linux上的uname命令,那么你就会认出来这个函数就是这个命令的一个接口。在Python 2上,它会返回一个包含系统类型(或者内核版本),主机名,版本,发布版本,机器的硬件以及处理器信息元组(tuple)。你可以使用下标访问个别属性,像这样:
>>> platform.uname()[0]
'Linux'
</div>
在Python 3上,这个函数返回的是一个命名元组:
>>> platform.uname()
uname_result(system='Linux', node='fedora.echorand',
release='3.7.4-204.fc18.x86_64', version='#1 SMP Wed Jan 23 16:44:29
UTC 2013', machine='x86_64', processor='x86_64')
</div>
因为返回结果是一个命名元组,这就可以简单地通过名字来指定特定的属性,而不是必须记住下标,像这样:
>>> platform.uname().system
'Linux'
</div>
platform模块还有一些上面属性的直接接口,像这样:
>>> platform.system()
'Linux'
>>> platform.release()
'3.7.4-204.fc18.x86_64'
</div>
linux_distribution()函数返回的有关你所在的linux发布版本的详细信息。例如,在Fedora 18系统上,这个命令会返回如下信息:
>>> platform.linux_distribution()
('Fedora', '18', 'Spherical Cow')
</div>
这个返回结果中包含了版本发布名,版本以及代号元组。特定的Python版本支持的发布版本上可以通过_supported_dists显示的值获得。
>>> platform._supported_dists
('SuSE', 'debian', 'fedora', 'redhat', 'centos', 'mandrake',
'mandriva', 'rocks', 'slackware', 'yellowdog', 'gentoo',
'UnitedLinux', 'turbolinux')
</div>
如果你的linux发布版本不在其中(或者其中之一的衍生发行版)。那么你很可能调用了上面这个函数而看不到任何有用的信息。
platform模块的最后一个函数,我们将会看看architecture()函数。当你无参的调用这个函数,它会返回包含架构位数以及python可执行的格式的元组,像这样:
>>> platform.architecture()
('64bit', 'ELF')
</div>
在32位的系统上,你将会看到:
>>> platform.architecture()
('32bit', 'ELF')
</div>
如果你指定了系统上其他任何可执行的,你都将会获得相似的结果,如同这样:
>>> platform.architecture(executable='/usr/bin/ls')
('64bit', 'ELF')
</div>
鼓励探索platform模块的除了这些的其它函数,找出你现在运行的Python版本。如果你想知道这个模块是如何获取这些信息的,你可以深入查看PYthon源码目录下的Lib/platform.py文件。
os和sys模块也可以获得一些系统属性,例如原生的字节序。接下来,我们超越Python标准库模块,去探索一些在linux系统通过proc和sysfs文件系统使之访问信息成为可能。注意的是通过文件系统来访问信息将会在不同的硬件架构有所不同。所以在读本文或是写脚本时要时刻记住可以试图从这些文件获取信息。
获取CPU信息
/proc/cpuinfo文件包含了你的系统处理器单元的信息。例如,这里就是python版的linux命令cat /proc/cpuinfo所做的事:
#! /usr/bin/env python
""" print out the /proc/cpuinfo
file
"""
from __future__ import print_function
with open('/proc/cpuinfo') as f:
for line in f:
print(line.rstrip('\n'))
</div>
当你使用Python 2 或者 Python 3执行这个程序时,你会在屏幕上看到所有/proc/cpuinfo的内容(在上面的程序里,rstrip()方法用来删除每行末尾的换行符)
在下面的代码里列举了使用startwith()字符串方法来显示你的处理器单元的模式。
#! /usr/bin/env python
""" Print the model of your
processing units
"""
from __future__ import print_function
with open('/proc/cpuinfo') as f:
for line in f:
# Ignore the blank line separating the information between
# details about two processing units
if line.strip():
if line.rstrip('\n').startswith('model name'):
model_name = line.rstrip('\n').split(':')[1]
 

