Ezri Brimhall 6d9b5c4f8a
Updated user report
Now uses an AWK one-liner, improving performance due to making fewer calls to getent. This
might be have been anissue with certain remote-user passwd backends that do not cache a list of users.

Additionally, added fallback to simply scan /etc/passwd if getent is not available on the system.
2024-10-02 13:33:08 -06:00

27 lines
1.2 KiB
Bash
Executable File

#!/bin/bash
# Returns a list of all users on the system with UID >= 1000 and their home directory and shell
# Sorted lexicographically by username
# Uses getent passwd so as to include remote users (e.g. LDAP) in the list
# getent passwd returns a list of all users on the system, local and remote
# it may return duplicates, since a user might be listed identically in multiple nsswitch databases (e.g. files and systemd)
# as such, we sort the list and remove duplicates with uniq before processing it
# I'm also removing the "nobody" user, since it's a special case and not a real user
# passwd entries have the following format:
# $username:x:$uid:$gid:$displayname:$home:$shell
# e.g. root:x:0:0::/root:/bin/bash
# Awk is 1-indexed, so $1 is the username, $2 is the x, $3 is the uid, etc.
# the fields we care about are:
# $1: username
# $3: uid
# $7: shell
if which getent &>/dev/null; then
getent passwd | sort | uniq | awk -F: '{if ($3 >= 1000 && $1 != "nobody") print "User: " $1 ", UID: " $3 ", Shell: " $7}'
else
echo "getent not found, falling back to /etc/passwd" >&2
awk -F: '{if ($3 >= 1000 && $1 != "nobody") print "User: " $1 ", UID: " $3 ", Shell: " $7}' /etc/passwd
fi