Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

printing out users without home directories

by codeout (Initiate)
on May 15, 2018 at 23:57 UTC ( [id://1214597]=perlquestion: print w/replies, xml ) Need Help??

codeout has asked for the wisdom of the Perl Monks concerning the following question:

open (USERS, '-|' , 'getent passwd' ) or die $!; @passwd_entries = <USERS>; close USERS;

This allows me to put all users into an array called @passwd_entries. How do I print out all users without /home directories?

Replies are listed 'Best First'.
Re: printing out users without home directories
by haukex (Archbishop) on May 16, 2018 at 06:28 UTC

    Perl has built-in functions for that, you don't need to shell out. You can find the documentation either via perldoc -f getpwent at the command line, or on http://perldoc.perl.org under endservent (because it splits the functions up a little too much). I took "without /home directories" to mean "with directories that aren't underneath /home" and I've incorporated the -d check suggested by syphilis to additionally show users who have a /home home directory that doesn't exist.

    use warnings; use strict; #use Data::Dumper; # Debug while (my @pwent = getpwent) { #print Dumper(\@pwent); # Debug my ($name,$uid,$home) = @pwent[0,2,7]; print "$name $uid $home\n" if $home !~ m{^/home/} || ! -d $home; }

    Or, with a nicer interface, there's User::pwent:

    use warnings; use strict; use User::pwent; while (my $pwent = getpwent) { print $pwent->name," ",$pwent->uid," ",$pwent->dir,"\n" if $pwent->dir !~ m{^/home/} || ! -d $pwent->dir; }

    If by "without /home directories" you meant that the field is blank, then you can change the condition to unless length $pwent->dir.

    Normally I'd also recommend using a module like Path::Class for the pathname handling, but I assumed you're on a *NIX system. Since I'm already at it, here's an example, with a little variation to the method calls, just because TIMTOWTDI:

    use warnings; use strict; use User::pwent; use Path::Class qw/dir/; my $home = dir('/home'); while (my $pwent = getpwent) { print join(' ', map {$pwent->$_} qw/name uid dir/ ),"\n" unless $home->subsumes( dir($pwent->dir) ); }
Re: printing out users without home directories
by huck (Prior) on May 16, 2018 at 03:30 UTC

    What does "user" mean? do system accounts qualify? The following tests all entries from getent passwd

    use strict; use warnings; my @passwd_entries; open (USERS, '-|' , 'getent passwd' ) or die $!; @passwd_entries = <USERS>; close USERS; for my $line (@passwd_entries){ my @parts=split(':',$line); unless ($parts[5]){print $parts[0].' '."No home listed\n";} else { unless (-d $parts[5]) {print $parts[0].' '."dir ".$parts[5]." no +t found\n";} } }
    see http://www.linfo.org/etc_passwd.html for the layout of the password file.

Re: printing out users without home directories
by syphilis (Archbishop) on May 16, 2018 at 02:47 UTC
    This allows me to put all users into an array called @passwd_entries. How do I print out all users without /home directories?

    for(@passwd_entries) { print "/home/$_ does not exist\n" unless -d "/home/$_"; }
    See -X and perlsyn.

    (Fixed a couple of typos in updates.)

    Cheers,
    Rob
      for(@passwd_entries) { print "/home/$_ ...

      In the context of the code from the root node, that won't work, since $_ contains the entire line from the entry.

        ... that won't work, since $_ contains the entire line from the entry

        Yes, the wording of the OP's initial post led me to believe that his @passwd_entries contained nothing but a list of the usernames - though, of course, the OP didn't exactly specify that was the case.

        Cheers,
        Rob

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1214597]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (1)
As of 2024-04-25 01:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found