📜  用于查找登录用户数的 Shell 脚本

📅  最后修改于: 2022-05-13 01:57:27.091000             🧑  作者: Mango

用于查找登录用户数的 Shell 脚本

每个操作系统都提供多用户帐户的功能。基于 Linux 的操作系统有一些命令或功能来检查用户帐户的详细信息并进行更改。此能力主要由作为 root 用户的 admin 帐户用户使用,为不同的用户提供权限和访问权限。管理员还可以检查当前登录的用户数量、注销的用户数量以及登录时间。在本文中,我们将探索所有这些方式,并编写一个 shell 脚本来高效地完成这些任务。

获取用户相关信息的命令:

1、id: id命令用于打印指定USER的用户和组信息。

-a              ignore, for compatibility with other versions
-Z, --context   print only the security context of the process
-g, --group     print only the effective group ID
-G, --groups    print all group IDs
-n, --name      print a name instead of a number, for -ugG
-r, --real      print the real ID instead of the effective ID, with -ugG
-u, --user      print only the effective user ID
-z, --zero      delimit entries with NUL characters, not whitespace

linux中的id命令

此 id 命令已生成所有用户标识符、组标识符和组。如果您只需要一个组标识符,请使用以下命令。

id -G

linux中的id -g



2. 组:这将打印指定用户所属的组。如果没有给出特定的用户名,它将搜索当前用户。对当前用户使用以下命令。

3. getent:此命令显示数据库中的条目。

-i, --no-idn            disable IDN encoding
-s, --service=CONFIG    Service configuration to be used
-?, --help              Give this help list
--usage                 Give a short usage message
-V, --version           Print program version

让我们看看我们系统上存在的 getent 程序的版本。

getent -V

linux中的getennt命令

4. lslogins:查看所有用户名和用户 ID。这提供了几个功能的列表,如 UID、用户、最后登录等。

-a, --acc-expiration      display info about passwords expiration
-c, --colon-separate      display data in a format similar to /etc/passwd
-e, --export              display in an export-able output format
-f, --failed              display data about the users' last failed logins
-G, --supp-groups         display information about groups
-g, --groups=     display users belonging to a group in 
-L, --last                show info about the users' last login sessions
-l, --logins=     display only users from 
-n, --newline             display each piece of information on a new line
--noheadings              don't print headings
--notruncate              don't truncate output

linux中的islogins命令linux中的islogins命令

5. users:该命令将打印登录到当前主机的用户名。



users

linux下的用户命令

这是当前唯一登录的用户。

6. 谁:显示谁已登录。这列出了具有 id 的用户以及用户登录的时间和日期。

-a, --all          same as –b, -d, --login, -p, -r, -t, -T, -u
-b, --boot         time of last system boot
-d, --dead         print dead processes
-H, --heading      print line of column headings
-l, --login        print system login processes
--lookup           attempt to canonicalize hostnames via DNS
-m                 only hostname and user associated with stdin
-p, --process      print active processes spawned by init
-q, --count        all login names and number of users logged on

7. w: w 命令显示登录的用户帐户并显示他们在做什么。

-h, --no-header   do not print header
-u, --no-current  ignore current process username
-s, --short       short format
-f, --from        show remote hostname field
-o, --old-style   old style output
-i, --ip-addr     display IP address instead of hostname (if possible)

这比 who 具有更多的功能和列,以提供有关用户的更多详细信息。

w

8. last 或 lastb: last 和 lastb 命令显示最后登录的用户列表

-             how many lines to show
-a, --hostlast        display hostnames in the last column
-d, --dns             translate the IP number back into a hostname
-f, --file      use a specific file instead of /var/log/wtmp
-F, --fulltimes       print full login and logout times and dates
-i, --ip              display IP numbers in numbers-and-dots notation
-n, --limit   how many lines to show
-R, --nohostname      don't display the hostname field
-s, --since 

这会根据日期和时间提供多个用户的所有登录详细信息。

last



9. lastlog:这将生成所有最近登录用户的报告。如果指定,这也可以创建单用户报告。

-b, --before DAYS              print only lastlog records older than DAYS
-C, --clear                    clear lastlog record of a user (usable only with -u)
-h, --help                     display this help message and exit
-R, --root CHROOT_DIR          directory to chroot into
-S, --set                      set lastlog record to the current time (usable only with -u)
-t, --time DAYS                print only lastlog records more recent than DAYS
-u, --user LOGIN               print lastlog record of the specified LOGIN

这讲述了用户的最新日志。

lastlog

最后记录

外壳脚本

现在我们将使用一些上述命令创建一个 shell 脚本来获取用户详细信息。我们正在以一种通过给定建议要求用户输入的方式来接近解决方案。然后,该输入将用于检查可用案例,然后将允许运行匹配的案例。

打开 gedit 文件:

根据您的喜好打开任何编辑器,我们使用 gedit 编辑器是因为它简单的用户界面和现有的颜色组合。

gedit userAccounts.sh

代码:

在 userAccounts.sh 中,我们将编写我们的代码,并使用 switch case 来比较用户输入。我们使用了 lslogins、who、groups 等命令来帮助我们满足用户需求。您可以在上面找到更多扩展这些命令的使用。那么,让我们开始编写脚本。

#!/bin/bash
#here we are you going to develope a script for various options on user accounts
echo -e "\n
[ 1 ] for listing all the user accounts name \n
[ 2 ] for counting the number of logged-in user accounts  \n
[ 3 ] for listing the names of currently logged-in users\n
[ 4 ] for checking the groups to which the current user belong \n"

#Now take user input
read userInput

#Now we will use switch cases for various input operations
case $userInput in
    1)
    #syntax lslogins 
    lslogins -o USER
    ;;
    2)
    #syntax who 

用于列出用户的 shell 脚本

授予可执行权限

必须向文件授予可执行权限才能使它们在系统上运行或执行。我们也可以在 chmod 命令中使用“777”而不是“+x”。另外请以root身份运行脚本

# chmod +x userAccounts.sh

使shell脚本可执行

运行脚本

./userAccounts.sh

示例 1:

./userAccounts.sh
1

脚本示例

示例 2:

./userAccounts.sh
2

脚本示例

示例 3:

./userAccounts.sh
3

脚本示例

因此,我们能够使用我们的 shell 脚本找出各种与登录相关的结果。