用于un*x系统的python utmp包装器
pyutmp的Python项目详细描述
简介
pyutmp模块为utmp 类Unix操作系统上的文件。套用linux程序员的 手动页面utmp(5),utmp文件允许用户发现信息 关于当前使用(即,已登录)系统的用户。utmp 文件是一系列条目,其结构通常由 utmp.hc头文件。
此模块为底层操作提供只读接口 系统的cutmpapi。
接口和用法
pyutmp模块提供两个类:utmpfile和utmp。一个 utmp file对象表示在 utmpfile对象,它产生连续的utmp对象。例如:
from pyutmp import UtmpFile import time for utmp in UtmpFile(): # utmp is a Utmp object if utmp.ut_user_process: print '%s logged in at %s on tty %s' % (utmp.ut_user, time.ctime(utmp.ut_time), utmp.ut_line)
utmpfile
除了__iter__()generator方法之外,还允许在 在utmp文件的内容中,utmp file类还提供了 rewind()方法,允许您将文件指针重置到顶部 文件的。有关详细信息,请参见类文档。
utmp
utmp类的字段依赖于操作系统。然而,他们 将始终至少包含以下字段:
Field | Type | Description |
---|---|---|
^{tt6}$ | ^{tt7}$ | The user associated with the utmp entry, if any. |
^{tt8}$ | ^{tt7}$ | The tty or pseudo-tty associated with the entry, if any. In this API, the line will always be the full path to the device. |
^{tt10}$ | ^{tt7}$ | The host name associated with the entry, if any. |
^{tt12}$ | timestamp | The timestamp associated with the entry. This timestamp is in the form returned by ^{tt13}$ and may be passed directly to methods like ^{tt14}$. |
^{tt15}$ | ^{tt16}$ | Whether or not the utmp entry is a user process (as opposed to a reboot or some other system event). |
在某些操作系统上,可能存在其他字段。例如,在 linux和solaris系统(以及其他系统v派生系统),utmp 包含以下字段:
Optional Field | Type | Description |
---|---|---|
^{tt17}$ | ^{tt7}$ | The type of the entry, typically one of the following string values: “RUN_LVL”, “BOOT_TIME”, “NEW_TIME”, “OLD_TIME”, “INIT_PROCESS”, “LOGIN_PROCESS”, “USER_PROCESS”, “DEAD_PROCESS”, “ACCOUNTING”. See the utmp(5) manual page for a description of these values |
^{tt19}$ | ^{tt20}$ | Associated process ID, if any. |
^{tt21}$ | ^{tt7}$ | The init(8) ID, or the abbreviated tty name. |
^{tt23}$ | ^{tt20}$ | Process exit code, if applicable. |
^{tt25}$ | ^{tt20}$ | Session ID, for windowing. |
^{tt27}$ | ^{tt20}$ array | IPv4 address of remote host (if applicable), one octet per array element. |
如果您正在编写可移植代码,则不应指望 这些属性——或者,至少,您应该将对它们的访问包装在 捕获AttributeError的try/catch块。
注释
此模块已在以下操作系统上测试:
- ubuntu linux,8.04版
- FreeBSD
- Mac OS X 10.4(老虎机)
- opensolaris(2008.05,x86,使用sunstudio 12编译器套件)
添加对其他unix变体的支持应该很简单。
限制
- 对utmp文件的访问是只读的。没有书面规定 到档案里去。
版权和许可
此软件是根据BSD许可证发布的,改编自 <;http://opensource.org/licenses/bsd-license.php>;
版权所有(c)2008-2010 Brian M.Clapper。 保留所有权利。
以源和二进制形式重新分配和使用,有无 如果满足以下条件,则允许修改:
- 重新发布源代码必须保留上述版权声明, 此条件列表和以下免责声明。
- 二进制形式的再分配必须复制上述版权声明, 文件中的条件列表和以下免责声明 和/或分发时提供的其他材料。
- “clapper.org”或其贡献者的名字都不能是 用于在没有 明确的事先书面许可。
本软件由版权所有者和贡献者“按原样”提供。 以及任何明示或默示保证,包括但不限于 适销性和特定用途的默示保证 不承认。在任何情况下,版权持有人或贡献者不得 对任何直接、间接、附带、特殊、惩戒性或 间接损害(包括但不限于 替代品或服务;使用、数据或利润损失;或业务 无论是何种原因造成的,根据任何责任理论,无论 合同、严格责任或侵权行为(包括疏忽或其他) 以任何方式使用本软件,即使已通知 可能造成这种损害。