如何解析/过滤“last”命令以显示 Ubuntu 14.04 中特定日期的登录?

如何解析/过滤“last”命令以显示 Ubuntu 14.04 中特定日期的登录?

我想解析当前日期的登录活动。Last 命令为我提供了多行和多列。如何在 Python 中过滤它甚至解析它?

任何帮助将不胜感激。

答案1

last提供以下选项:

-s, --since time
              Display  the  state of logins since the specified time.  This is useful, e.g., to easily determine who was logged in at a particular time.
              The option is often combined with --until.
-t, --until time
              Display the state of logins until the specified time.
-p, --present time
              Display the users who were present at the specified time.  This is like using the options --since and --until together with the same time.

有关时间格式,手册页说:

The options that take the time argument understand the following formats:

       YYYYMMDDhhmmss
       YYYY-MM-DD hh:mm:ss
       YYYY-MM-DD hh:mm      (seconds will be set to 00)
       YYYY-MM-DD            (time will be set to 00:00:00)
       hh:mm:ss              (date will be set to today)
       hh:mm                 (date will be set to today, seconds to 00)
       now
       yesterday             (time is set to 00:00:00)
       today                 (time is set to 00:00:00)
       tomorrow              (time is set to 00:00:00)
       +5min
       -5days

这里有一些例子:

last -s 2017-08-04           # from 2017-08-04 until today
last -s yesterday -t -120min # from yesterday until two hours ago
last -s -4days -t -2days     # from four days ago until two days ago
last -p 2017-08-04           # users present at 2017-08-04 00:00:00

阅读更多关于last及其选项的信息手册页

答案2

您可以将 的输出通过管道传输lastgrep。如果您想要查看自 8 月 4 日星期五以来的所有登录信息,请使用:

last | grep 'Fri Aug [ ]*4'

grep如果您希望查看其他日期,请相应地调整您的日期字符串。

相关内容