RookieRoo
RookieRoo

Reputation: 301

Extract linux (admin) users report for remote hosts

I am trying to find the best way to extract a report of Linux users and also which one of them are admin. The targets are Debian (8,9) and Ubuntu (18,20). The final report would be very long, so I want the hostname in the first line, to identify each VM. I also want a short&bried report, with only the username, not all the details..

My code here is bringing some irelevant info , when pushing it to multiple hosts (via pssh):

#!/bin/bash

_l="/etc/login.defs"
_p="/etc/passwd"

## get mini UID limit ##
l=$(grep "^UID_MIN" $_l)

## get max UID limit ##
l1=$(grep "^UID_MAX" $_l)

echo ""
echo `hostname`
echo ""
echo "----------[ User Accounts ]---------------"
awk -F':' -v "min=${l##UID_MIN}" -v "max=${l1##UID_MAX}" '{ if ( $3 >= min && $3 <= max  && $7 != "/sbin/nologin" ) print $0 }' "$_p"
#users with UID >= 1000 (MIN) and UID <= 60000 (MAX)
#these users are allowed to login into system if shell is bash/csh/tcsh/ksh as defined in the /etc/shells file

echo "----------[ from which, Admin Accounts ]---------------"
echo `grep '^sudo:.*$' /etc/group | cut -d: -f4`
#users in sudoers group

exit 0

It throws garbage like:

Thank you for your suggestions.

Upvotes: -1

Views: 227

Answers (1)

Martin Tovmassian
Martin Tovmassian

Reputation: 1468

To select only the username from the output your awk command gives you, you should print the field number 1 rather than the field number 0:

awk -F':' -v "min=${l##UID_MIN}" -v "max=${l1##UID_MAX}" '{ if ( $3 >= min && $3 <= max  && $7 != "/sbin/nologin" ) print $1 }' "$_p"

Upvotes: 0

Related Questions