Linux下last命令如何显示年份(last_patch)
2012-06-18 10:26:57   来源:我爱运维网   评论:0 点击:

在审计服务器业务时,因历史原因,某些Linux服务器不确定是否在使用或多长时间内有人登录过,我们一般会使用Linux下的last命令来查看。但很...
在审计服务器业务时,因历史原因,某些Linux服务器不确定是否在使用或多长时间内有人登录过,我们一般会使用Linux下的last命令来查看。

但很遗憾的是,last命令不会显示年份,只会显示下列格式的数据:
[user1@host-110158 ~]$ last
user1 pts/0 211.144.198.146 Mon Jun 18 10:34 still logged in
user1 pts/0 61.171.0.93 Sat Jun 16 22:43 - 23:16 (00:32)
user1 pts/0 61.171.0.93 Fri Jun 15 21:07 - 01:42 (04:35)
user1 pts/0 211.144.198.146 Fri Jun 15 16:12 - 18:24 (02:12)
user3 pts/0 58.62.39.166 Thu Jun 14 21:08 - 22:25 (01:16)
user3 pts/1 210.21.50.156 Thu Jun 14 08:45 - 15:27 (06:42)
user3 pts/0 210.21.50.156 Thu Jun 14 08:08 - 10:44 (02:35)
user3 pts/0 210.21.50.157 Tue Jun 12 09:46 - 14:11 (04:25)

这样就不知道,最后登录是哪一年的数据。

一种方法是使用last -t 参数
last -t 20100101010101
显示2010-01-01 01:01:01之前的登录信息,将该信息与last对比得比,也是很不方便。

更佳的解决方案是使用下列代码进行编译生成一个last_patch命令
last_patch.c代码:
#include <stdio.h>
#include <utmp.h>
#include <string.h>
#include <time.h>

int main(void) {
        struct utmp *line;
        time_t timestamp;
        utmpname("/var/log/wtmp");
        setutent();
        while( (line = getutent()) != NULL) {
                if (line->ut_type == USER_PROCESS ) {
                        timestamp = line->ut_tv.tv_sec;
                        printf("%s %s", line->ut_user, asctime(localtime(&timestamp)));
                }
        }
        endutent();
        return 0;
}

编译生成执行命令last_patch:
$ gcc -Wall last_patch.c -o last_patch

将last_patch放入/usr/local/bin目录。
运行last_patch,显示内容格式如下:
-bash-3.2$ last_patch
user1 Fri Jun 15 16:19:54 2012
user2 Fri Jun 15 17:31:40 2012
user3 Mon Jun 18 10:57:15 2012

相关热词搜索:Linux last 命令

上一篇:XEN下从Dom0进入虚拟机的单用户模式
下一篇:Linux开启NTP会在2012年06月遇闰秒BUG可能导致服务器重启

分享到: 收藏
评论排行