2021年7月25日 星期日

[ Bash 範例代碼 ] List logged user by date

 Source From Here

09-1_list_of_logged_users.sh
  1. #!/bin/bash  
  2. # Author: John Lee  
  3. # Date: 2021/07/25  
  4. # Description: This script will list users logged in by date  
  5. # Info:  
  6. #   $ last | head -n 3  
  7. #   root     pts/1        192.168.37.1     Sun Jul 25 00:16    gone - no logout  
  8. #   root     pts/1        192.168.37.1     Sat Jul 24 18:41 - 20:33  (01:51)  
  9. #   root     pts/1        192.168.37.1     Fri Jul 23 23:00 - 05:13  (06:13)  
  10.   
  11. # e.g.: Sun Jul 25  
  12. today=`date | awk '{print $1,$2,$3}'`  
  13.   
  14. echo -n "Give month to search logged user(e.g: `date +'%b'`): "  
  15. read month  
  16. echo -n "Give day to search logged user(e.g.: `date +'%d'`): "  
  17. read day  
  18.   
  19. if [ ! -z "$month" ] && [ ! -z "$day" ]; then  
  20.   tdate="$month\s+$day\s+"  
  21. else  
  22.   tdate=$today  
  23.   month=`date +'%b'`  
  24.   day=`date +'%d'`  
  25. fi  
  26.   
  27. echo "Search logged user on date='$month $day'"  
  28. for u in `last | grep -P "${tdate}" | awk '{print $1}' | sort | uniq`  
  29. do  
  30.   echo "Found user '$u'"  
  31. done  
Execution output:
# last | grep -P 'Mon May\s+3\s+'
root pts/1 192.168.37.1 Mon May 3 23:08 - 01:32 (02:24)
root pts/0 192.168.37.1 Mon May 3 22:18 - 03:49 (05:30)
john :0 :0 Mon May 3 22:18 - crash (13+01:54)
...


# ./09-1_list_of_logged_users.sh
Give month to search logged user(e.g: Jul): May
Give day to search logged user(e.g.: 25): 3
Search logged user on date='May 3'
Found user 'john'
Found user 'reboot'
Found user 'root'
Found user 'wtmp'


沒有留言:

張貼留言

[Git 常見問題] error: The following untracked working tree files would be overwritten by merge

  Source From  Here 方案1: // x -----删除忽略文件已经对 git 来说不识别的文件 // d -----删除未被添加到 git 的路径中的文件 // f -----强制运行 #   git clean -d -fx 方案2: 今天在服务器上  gi...