有没有办法确定例如当天是本月的第一次登录?我正在尝试在我的脚本中使用它。我很熟悉,last
但它似乎不是这项工作的命令。
让我们举个例子:
- 如果今天是本月第一次登录,则回显“第一次!”
- 否则回应“不”
示例场景:
7 月 2 日是我 7 月份的第一次登录,那天我登录了 5 次。第一次登录时它只能显示“first”,而后续 4 次登录时只能显示“nope”。
答案1
使用last
和awk
last -R "$USER" | \
perl -ne 'print unless /wtmp\sbegins/ || /^$/' | \
awk 'END {print $4,$5,$6}'
last
搜索该文件/var/log/wtmp
(或标志指定的文件-f
)并显示自该文件创建以来登录(和退出)的所有用户的列表。
默认情况下/var/log/wtmp
每个月都会创建:
% awk '/\/var\/log\/wtmp/,/}/' /etc/logrotate.conf
/var/log/wtmp {
missingok
monthly
create 0664 root utmp
rotate 1
}
last -R $USER
返回/var/log/wtmp
给定用户的所有条目。第一个登录名位于输出的底部,因此END
在我的awk
命令中。
% last -R "$USER"
aboettge pts/1 Wed Jul 15 12:04 still logged in
aboettge pts/1 Wed Jul 15 11:29 - 12:04 (00:34)
aboettge pts/1 Wed Jul 15 08:08 - 11:29 (03:21)
aboettge pts/1 Wed Jul 15 08:08 - 08:08 (00:00)
aboettge pts/1 Wed Jul 15 08:07 - 08:08 (00:00)
aboettge tty2 Wed Jul 15 08:03 still logged in
aboettge pts/1 Tue Jul 14 12:58 - down (00:53)
aboettge pts/1 Tue Jul 14 09:06 - 12:58 (03:51)
aboettge pts/1 Tue Jul 14 09:03 - 09:06 (00:02)
aboettge pts/1 Tue Jul 14 08:23 - 09:03 (00:40)
aboettge pts/1 Tue Jul 14 08:22 - 08:23 (00:00)
aboettge tty2 Tue Jul 14 08:22 - down (05:28)
aboettge pts/1 Mon Jul 13 12:01 - 16:26 (04:24)
aboettge pts/1 Mon Jul 13 11:49 - 11:59 (00:10)
aboettge pts/1 Mon Jul 13 11:46 - 11:49 (00:02)
aboettge tty2 Mon Jul 13 11:44 - down (04:42)
aboettge pts/2 Mon Jul 13 11:26 - 11:35 (00:08)
aboettge tty2 Mon Jul 13 11:25 - down (00:09)
aboettge pts/3 Fri Jul 10 14:26 - 14:28 (00:01)
aboettge pts/3 Fri Jul 10 14:23 - 14:26 (00:03)
aboettge pts/2 Fri Jul 10 13:34 - 15:27 (01:53)
aboettge pts/2 Fri Jul 10 13:31 - 13:31 (00:00)
aboettge pts/2 Fri Jul 10 13:25 - 13:30 (00:05)
aboettge pts/2 Fri Jul 10 10:28 - 13:19 (02:50)
aboettge pts/1 Fri Jul 10 08:35 - 15:27 (06:52)
aboettge tty2 Fri Jul 10 08:00 - down (07:28)
aboettge pts/1 Thu Jul 9 16:25 - 16:27 (00:01)
aboettge tty2 Thu Jul 9 16:21 - down (00:06)
aboettge tty1 Thu Jul 9 16:03 - crash (00:00)
aboettge tty1 Thu Jul 9 15:29 - 16:03 (00:33)
aboettge pts/1 Thu Jul 9 14:32 - 14:32 (00:00)
aboettge pts/1 Thu Jul 9 08:51 - 08:51 (00:00)
aboettge pts/1 Thu Jul 9 08:51 - 08:51 (00:00)
aboettge pts/1 Thu Jul 9 08:50 - 08:51 (00:00)
aboettge pts/1 Thu Jul 9 08:46 - 08:46 (00:00)
aboettge pts/1 Thu Jul 9 08:46 - 08:46 (00:00)
aboettge pts/1 Thu Jul 9 08:45 - 08:45 (00:00)
aboettge pts/1 Thu Jul 9 08:44 - 08:44 (00:00)
aboettge pts/1 Thu Jul 9 08:42 - 08:42 (00:00)
aboettge pts/1 Thu Jul 9 08:42 - 08:42 (00:00)
aboettge pts/1 Thu Jul 9 08:42 - 08:42 (00:00)
aboettge pts/1 Thu Jul 9 08:41 - 08:41 (00:00)
aboettge pts/1 Thu Jul 9 08:41 - 08:41 (00:00)
aboettge pts/1 Thu Jul 9 08:40 - 08:41 (00:00)
aboettge pts/1 Thu Jul 9 08:39 - 08:39 (00:00)
aboettge pts/1 Thu Jul 9 08:38 - 08:38 (00:00)
aboettge pts/1 Thu Jul 9 08:36 - 08:36 (00:00)
aboettge pts/1 Thu Jul 9 08:36 - 08:36 (00:00)
aboettge pts/1 Thu Jul 9 08:36 - 08:36 (00:00)
aboettge pts/1 Thu Jul 9 08:35 - 08:35 (00:00)
aboettge pts/1 Thu Jul 9 08:35 - 08:35 (00:00)
aboettge pts/2 Thu Jul 9 07:50 - 16:03 (08:12)
aboettge :0 Thu Jul 9 07:50 - crash (08:13)
aboettge pts/1 Wed Jul 8 16:13 - 16:27 (00:13)
aboettge pts/2 Wed Jul 8 13:47 - 16:27 (02:39)
aboettge pts/1 Wed Jul 8 11:30 - 14:14 (02:44)
aboettge pts/1 Wed Jul 8 08:18 - 11:29 (03:11)
aboettge :0 Wed Jul 8 08:02 - crash (23:47)
aboettge pts/2 Tue Jul 7 08:54 - 08:55 (00:00)
aboettge pts/1 Tue Jul 7 08:02 - 13:13 (05:10)
aboettge :0 Tue Jul 7 07:50 - down (05:23)
aboettge pts/1 Mon Jul 6 09:55 - 14:48 (04:53)
aboettge :0 Mon Jul 6 08:27 - crash (23:19)
aboettge pts/2 Fri Jul 3 07:52 - 15:30 (07:38)
aboettge :0 Fri Jul 3 07:51 - down (07:40)
aboettge pts/2 Thu Jul 2 15:16 - 15:18 (00:01)
aboettge pts/2 Thu Jul 2 15:07 - 15:07 (00:00)
aboettge pts/1 Thu Jul 2 14:46 - 16:21 (01:35)
aboettge pts/1 Thu Jul 2 09:53 - 09:53 (00:00)
aboettge pts/2 Thu Jul 2 08:00 - 14:41 (06:40)
aboettge :0 Thu Jul 2 08:00 - crash (23:47)
aboettge pts/5 Wed Jul 1 14:16 - 14:38 (00:21)
aboettge pts/3 Wed Jul 1 14:01 - 16:10 (02:09)
aboettge pts/2 Wed Jul 1 13:27 - 16:10 (02:42)
aboettge pts/1 Wed Jul 1 13:22 - 16:10 (02:48)
aboettge :0 Wed Jul 1 13:15 - 16:21 (03:05)
aboettge pts/2 Wed Jul 1 11:30 - 11:36 (00:06)
aboettge pts/1 Wed Jul 1 11:29 - 13:02 (01:32)
aboettge pts/1 Wed Jul 1 11:27 - 11:29 (00:01)
aboettge pts/2 Wed Jul 1 08:28 - 11:27 (02:58)
wtmp begins Wed Jul 1 08:28:18 2015
例子
% last -R "$USER" | perl -ne 'print unless /wtmp\sbegins/ || /^$/' | awk 'END {print $4,$5,$6}'
Jul 1 08:28
分解
% last -R "$USER"
往上看
perl -ne 'print unless /wtmp\sbegins/ || /^$/'
删除空行并
wtmp begins …
awk 'END {print $4,$5,$6}'
从最后一行打印字段 4、5 和 6,默认分隔符是
(空格)
答案2
您可以使用来解析当前wtmp
日志文件rawtmp
。
首先安装该工具:
sudo apt-get install sac
然后使用以下命令:
(rawtmp -s 07/01/15; rawtmp -s 07/01/15 -w /var/log/wtmp.1) \
| grep 'LOGIN Process' | sort | head -n 1 | awk -F: '{print $1}' \
| perl -pe 's/(\d+)/localtime($1)/e'
对我来说它输出:
Wed Jul 1 08:40:58 2015
为确保获取整月的条目,该命令调用rawtmp
两次。第二次使用已保存的日志轮换 ( /var/log/wtmp.1
)。
wtmp
旋转设置由 ubuntu 管理logrotate
,默认情况下它们存储在/etc/logrotate.conf
:
# no packages own wtmp, or btmp -- we'll rotate them here
/var/log/wtmp {
missingok
monthly
create 0664 root utmp
rotate 1
}
答案3
last <username> | tac | awk '{
if ( b != $5 && NR !=1 ){
b=$5; print $4,$5,$6,$7,$8,$9,$10
}
}'
这将显示每月的首次登录last
。
这里tac
将会反转last
输出。
awk
将仅显示第 5 列中新字符串的第一次出现,即一个月内的第一次登录。
NR !=1
将消除wtmp begins
线。
答案4
如果脚本在登录后仅运行一次,则不依赖last
或 的方法是创建一个空文件。下次运行时,如果其创建时间早于当前月份的第一刻,或者文件不存在,则。无论哪种情况,都使用 更新或创建文件以供下次使用。/var/log/wtmp
ècho "first!"
touch
MONTH=`/bin/tempfile`
touch -d "01 $(date +'%b %y')" "$MONTH"
if [ ~/.lastlogin -ot "$MONTH" ]
then echo "first!"
fi
rm "$MONTH"
touch ~/.lastlogin