Bash 指令確定特定時間段內的首次登入

Bash 指令確定特定時間段內的首次登入

有沒有辦法確定例如當天是本月的第一次登入?我正在嘗試在我的腳本中使用它。我很熟悉,last但似乎不是這項工作的命令。

我們以此為例:

  • 如果今天是本月第一次登錄,則回顯“first!”
  • 否則回顯“不”

範例場景:

7月2日是我7月第一次登錄,那天我登入了5次。它只能在我第一次登入時回顯“first”,在隨後的 4 次登入時回顯“nope”。

答案1

使用lastawk

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

相關內容