您的位置:首页 > 运维架构 > Shell

Shell命令编写——查看当前登陆用户的登录时间ac,last

2011-11-30 00:42 537 查看
ac命令根据当前的/var/log/wtmp文件中的登录进入和退出来报告用户连接的时间(小时),

如果不使用标志,则报告总的时间.

1.键入ac命令,然后按回车键,将显示如下内容: total 496.35

2.键入ac -d命令,然后按回车键,将显示每天的总的连接时间:

Aug 12 total 261.87

Aug 13 total 351.39

Aug 14 total 396.09

Today total 179.02

键入ac -p命令,然后按回车键,将显示每个用户的总的连接时间:

tom 0.32

root 496.05

total 496.37

编写ac

1.#man ac

2.#man 5 wtmp

发现也存储在utmp.h中,并查找到

#define WTMP_FILE _PATH_WTMP

因此打开WTMP_FILE文件/var/log/wtmp中来读取utmp结构的信息

根据我们前面编写who的方法,我决定套用前面的结构,重点是对show_info函数的重写

对于-d和-p以后再进行实现,只实现#ac 显示当前用户的登录时间总计

Its format is exactly like utmp except that a null user name indicates a logout on the associated terminal.

倒数两列的计算比较特殊

reboot system boot 2.6.18-92.el5 Wed Mar 23 14:26 (00:06)

root :0 Wed Mar 23 14:19 - 14:24 (00:04)

root :0 Wed Mar 23 14:19 - 14:19 (00:00)

reboot system boot 2.6.18-92.el5 Wed Mar 23 14:19 (00:05)

reboot system boot 2.6.18-92.el5 Wed Mar 23 22:14 (-7:-56)

首先reboot system boot 2.6.18-92.el5 Wed Mar 23 14:19 (00:05) (00:05)=reboot时间 14:19-文件向下读取的第一个shutdown的时间

root :0 Wed Mar 23 14:19 - 14:19 (00:00)

(00:00) = 当前表项的时间 - 文件向下读取过程中该utmp结构下一个终端类型相同表项的时间
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: