我需要知道特定用户的login历史logging(即login和注销时间),如何在Linux中为特定date范围提取此历史logging?
你可以试试last
命令:
last john
它打印出用户john的login/离开历史logging。 而正在运行
last
打印所有用户的login/离开历史logging。
如果你需要在历史上比一个月更远的话,你可以用last
命令读取/var/log/wtmp.1
文件。
last -f wtmp.1 john
将显示上个月用户john
的login历史logging。
最后一个日志输出不是太重,也比较容易parsing,所以我可能会将输出传给grep来寻找一个特定的date模式。
last john | grep -E 'Aug (2[0-9]|30) '
last john | grep -E 'Aug (2[0-9]|30) '
8月20日至last john | grep -E 'Aug (2[0-9]|30) '
日。 或者类似的东西:
last -f /var/log/wtmp.1 john | grep -E 'Jul (1[0-9]|2[0-9]|30) '
last -f /var/log/wtmp.1 john | grep -E 'Jul (1[0-9]|2[0-9]|30) '
7月10日至last -f /var/log/wtmp.1 john | grep -E 'Jul (1[0-9]|2[0-9]|30) '
日为用户john
。
如何在Linux中提取特定date范围的login历史logging?
列出所有用户从25日至28日/ 8月login的示例:
last | while read line do date=`date -d "$(echo $line | awk '{ print $5" "$6" "$7 }')" +%s` [[ $date -ge `date -d "Aug 25 00:00" +%s` && $date -le `date -d "Aug 28 00:00" +%s` ]] && echo $line done
awk '{ print $5" "$6" "$7 }'
从last
输出中提取对应列的date时间 +%s
将date时间转换为纪元时间 -ge
代表大于或等于 -le
表示小于或等于 您也可以使用last <username>
为特定用户执行此操作。