查找在特定日期从特定计算机登录的所有用户

Find all users that logged in at a specific date and from a specific machine

我想编写一个 bash 脚本,它将两个参数作为输入 (day hostname) 并输出 username 当天登录的所有用户当前月份 并且来自 hostname 的地址。

到目前为止,我已经编写了这段代码:

#!/bin/bash

#parameter correction checking
if [ ! $# -eq 2 ]
then echo "You have input the wrong number of parameters!"
fi

#Current month checking
CurrentMonth=`date | cut -d' ' -f2`
#This will contain the abbreviation of the current month: e.g "Mar"

# -> IP/hostname;  -> day (of the current month)
last | grep '' | grep '$CurrentMonth ' | cut -d' ' -f1
#here the two greps will get the lines that check out our requirements, and the cut will return only the username

我做错了什么? 如果我将 CurrentMonth 变量替换为 Mar 并将 </code> 替换为实际的 <code>IP address

,代码运行得很好

单引号 ('') 之间的所有内容都将被视为文字文本,并且不会扩展变量。而是对包含变量的字符串使用双引号 ("")。

请注意,如果您使用 bash -x yourscript 启动它或将 shebang 更改为 #!/bin/bash -x

,您始终可以调试 bash 脚本并获得显示的扩展命令

您的脚本的工作版本如下所示:

#!/bin/bash

#parameter correction checking
if [ ! $# -eq 2 ]
then echo "You have input the wrong number of parameters!"
fi

#Current month checking
CurrentMonth=`date | cut -d' ' -f2`
#This will contain the abbreviation of the current month: e.g "Mar"

# -> IP/hostname;  -> day (of the current month)
last | grep "" | grep "$CurrentMonth " | cut -d' ' -f1
#here the two greps will get the lines that check out our requirements, and the cut will return only the username



备注:

last 默认打印用户名缩写,您可能需要添加 -w 标志来打印全名。

另请注意,您可以使用 lasts 内置函数按时间过滤,而不是 grep

-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.
TIME FORMATS
       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              │                                            │
       └────────────────────┴────────────────────────────────────────────┘