How can I run passwd -S and write its output to Perl?

I want to scan the passwd file and print only the lines if the user is not blocked. That is, it passwd -S $userdoes not return "Password: locked." I can easily do in ksh. What is the best way to do this in Perl?

+3
source share
4 answers

Same as single line.

sudo perl -F: -lane 'print $F[0] if $F[1]!~/^!/' /etc/shadow
+2
source

Earlier in this answer, it was told how to print only blocked users. The bug is fixed.

On Linux, a locked user account has a password starting with '!'. You can independently analyze the file /etc/shadow, separated by a colon:

# Run as root with /etc/shadow as program argument
while (<>) {
    chomp;
    my ($user, $password, $remainder) = split /:/, $_, 3;
    print $user."\n" unless $password =~ /^!/;
}

: , - getpwent:

# Must run as root
while (my ($user, $password) = getpwent) {
    print $user."\n" unless $password =~ /^!/;
}

.. root, /etc/shadow , .

+1

, , , passwd -S , (, , ), :

cat /etc/passwd | perl -ne '$user = (split /:/)[0]; print "$user\n" if `passwd -S $user` ne "Password: locked"'

, , - , :

`passwd -S $user` !~ /^\s*Password\s*:\s*locked\s*$/im'

"i" (, , ), "m" [ passwd -S]. , , , ( * nix).

, cygwin , :

`passwd -S $user` !~ /^\s*Account\s+disabled\s+:\s+yes\s*$/im'
0
source
open(F,"<","/etc/shadow") or die "Cannot open shadow file:$!\n";
while(<F>){
    chomp;
    @s = split /:/;
    if ( $s[1] !~ /!/){
        print "user: $s[0] not locked \n";
    }
}
close(F);

use as root.

0
source

Source: https://habr.com/ru/post/1734216/


All Articles