Operating System - HP-UX
1833768 Members
1826 Online
110063 Solutions
New Discussion

Anyone already have a script to do this?

 
SOLVED
Go to solution
S-un-B-ix-S
Advisor

Anyone already have a script to do this?

We are trying to clean up our password files, and it has come to light that there are a large number of users that do not have a valid GID value.

Does anyone have a script that go through each user and check whether their GID is valid (per /etc/groups) and if it not, put that output into a list?
4 REPLIES 4
James R. Ferguson
Acclaimed Contributor
Solution

Re: Anyone already have a script to do this?

Hi:

This will help:

# cat ./badgid
#!/usr/bin/perl
use strict;
use warnings;

my %gids;
my ( $name, $uid, $gid );

while ( ( undef, undef, $gid ) = getgrent ) {
$gids{$gid}++;
}
while ( ( $name, undef, $uid, $gid ) = getpwent ) {
print "uid $uid for user '$name' with gid $gid is bogus\n"
unless defined $gids{$gid};
}
1;

...This script caches the 'gid' of the group database and then proceeds to sequentially examine every user record's 'gid'.

Silenece is golden. That is, the last of output means that all 'gid's are valid. Unmatched records are produce:

uid 1002 for user 'dummy' with gid 220 is bogus

Regards!

...JRF...
Jeff_Traigle
Honored Contributor

Re: Anyone already have a script to do this?

Not tested, but something like this should work:

#!/usr/bin/sh

USERLIST=/var/tmp/users_with_invalid_gid.txt
USERS=$(awk -F: '{print $1}' /etc/passwd)

for USER in ${USERS}
do
GID=$(grep ^${USER}: /etc/passwd | awk -F: '{print $4}')
if [ $(grep -c :${GID}: /etc/group) -eq 0 ]
then
echo ${USER} >> ${USERLIST}
fi
done
--
Jeff Traigle
Peter Nikitka
Honored Contributor

Re: Anyone already have a script to do this?

Hi,

the command 'pwck' - part of the OS - will be a help for you as well. Just check the man page!

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
James R. Ferguson
Acclaimed Contributor

Re: Anyone already have a script to do this?

Hi (again):

The 'pwck' and/or 'grpck' may not discern inter user-group file inconsistencies.

Hence, my simple Perl script.

Regards!

...JRF...