Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

use getpwent to find users and delete them

by garcimo (Novice)
on May 17, 2018 at 17:00 UTC ( #1214762=perlquestion: print w/replies, xml ) Need Help??

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

I am looking to clean up some server old users in a environment that unfortunately does not use ldap yet. I have a list of users that i need to check in the servers and if they exist remove them. I was trying to test with this code:
$userdelex = "sudo /usr/sbin/userdel"; # location of userdel executab +le sub DeleteUnixAccount{ my ($account,$record) = @_; ### construct the command line, using: # -r = remove the account's home directory for us my @cmd = ($userdelex, "-r", $account); print "@cmd\n"; print "Deleting account..."; my $result = 0xffff & system @cmd; # the return code is 0 for success, non-0 for failure, so we inver +t if (!$result){ print "succeeded.\n"; return ""; } else { print "failed.\n"; return "$userdelex failed"; } } DeleteUnixAccount(toto);
I was thinking of user user:pwent or getpwent() in order to search if the user is present on the server. I will be connecting to the server in ssh using Net::OpenSSH::Parallel any idea how to use getpwent or user:pwent to do this? thanks in advance.

Replies are listed 'Best First'.
Re: use getpwent to find users and delete them
by roboticus (Chancellor) on May 17, 2018 at 17:35 UTC

    garcimo:

    The documentation (perldoc -f getpwent) indicates that getpwent returns a list of values like:

    my ( $name, $passwd, $uid, $gid, $qutoa, $comment, $gcos, $dir, $shell +, $expire ) = getpwent;

    and, if the user doesn't exist, it returns a "single meaningless true value". So I'd suggest calling getpwent, and then checking one of the values after the first one to see if it's defined or not. If not, the user ought not exist. So something like:

    my ( $name, $passwd, $uid, $gid, $qutoa, $comment, $gcos, $dir, $shell +, $expire ) = getpwent($account); # An account *must* have a UID, so let's check that if ( defined $uid ) { print "User $account uid=$uid. OK to delete user!\n"; } else { print "User $account does not exist...\n"; }

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

      Hello sadly it does not work. executing this:
      #!/usr/bin/perl use warnings; use strict; my $account = "tata"; my ( $name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell +, $expire ) = getpwent($account); if ( defined $uid ) { print "User $account uid=$uid. OK to delete user!\n"; } else { print "User $account does not exist...\n"; }
      gives me errors:

      syntax error at ./users_detect.pl line 9, near "($account" Execution of ./users_detect.pl aborted due to compilation errors.

      if I do not put the getpwent($account) it compiles but always returns uid=0 whatever the account

        I think getpwent expects no parameter. It's unfortunate that it (or Perl) doesn't say so...

        Changing your code to use getpwnam instead works for me:

        my ( $uid ) = getpwnam($account);

        garcimo:

        Sorry about that, I thought that I tested that, but it's obvious that I didn't. (Generally, I try to test my assumptions before converting a preview into an actual post, but it appears I made a mistake this time!)

        Good thing that Corion++ noticed the error and showed you the appropriate fix.

        ...roboticus

        When your only tool is a hammer, all problems look like your thumb.

Re: use getpwent to find users and delete them
by haukex (Bishop) on May 17, 2018 at 22:42 UTC

    If you use system LIST with the list having more than one element, then your command line arguments need to be separate elements, otherwise you're currently trying to run a program named literally "sudo /usr/sbin/userdel". Try my @cmd = ("sudo", "/usr/sbin/userdel", "-r", $account);.

Re: use getpwent to find users and delete them
by Anonymous Monk on May 19, 2018 at 12:12 UTC
    Can't you grep /etc/passwd?
      why do that when it's built in to Perl?

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (6)
As of 2020-11-30 23:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?