如果 /etc/passwd 对应的影子条目是 999999,如何从 /etc/passwd 中取出一行?

发布于 2024-08-22 08:41:52 字数 140 浏览 5 评论 0原文

我想将 passwd 文件中的每个用户与影子文件中的条目进行比较,如果影子文件中的条目与 999999 匹配,则打印出 passwd 文件的整行。Perl 中最简单的方法是什么?或者我想我可以 awk 从一个文件中取出值并在另一个文件中匹配?这样做的最佳方法是什么?

I want to compare each user in the passwd file with his entry in the shadow file, and print out the whole line of the passwd file if the entry in the shadow file matches 999999. What is the easiest way in Perl to do this? Or I suppose I could awk the values out of one file and match in the other file? What is the best way of doing this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

深者入戏 2024-08-29 08:41:52
awk -F":" 'FNR==NR&&$5=="99999"{user[$1];next}($1 in user)' /etc/shadow /etc/passwd

FNR==NR&&$5=="99999" 更改为 FNR==NR&&$5=="99999"&&$2!="!!"< /code> 如果你想排除带“!!”的行

awk -F":" 'FNR==NR&&$5=="99999"{user[$1];next}($1 in user)' /etc/shadow /etc/passwd

change FNR==NR&&$5=="99999" to FNR==NR&&$5=="99999"&&$2!="!!" if you want to exclude lines with "!!"

柠北森屋 2024-08-29 08:41:52
#! /usr/bin/perl

use warnings;
use strict;

sub read_passwd {
  open my $fh, "<", "/etc/passwd" or die "$0: open: $!";

  my %passwd;
  while (<$fh>) {
    next unless /^([^:]+)/;
    $passwd{$1} = $_;
  }

  \%passwd;
}

my $passwd = read_passwd;

open my $fh, "<", "/etc/shadow" or die "$0: open: $!";
while (<$fh>) {
  my($user,$maxage) = (split /:/)[0,4];
  next unless $maxage eq 99999;
  if ($passwd->{$user}) {
    print $passwd->{$user};
  }
  else {
    warn "$0: no passwd entry for '$user'";
  }
}
#! /usr/bin/perl

use warnings;
use strict;

sub read_passwd {
  open my $fh, "<", "/etc/passwd" or die "$0: open: $!";

  my %passwd;
  while (<$fh>) {
    next unless /^([^:]+)/;
    $passwd{$1} = $_;
  }

  \%passwd;
}

my $passwd = read_passwd;

open my $fh, "<", "/etc/shadow" or die "$0: open: $!";
while (<$fh>) {
  my($user,$maxage) = (split /:/)[0,4];
  next unless $maxage eq 99999;
  if ($passwd->{$user}) {
    print $passwd->{$user};
  }
  else {
    warn "$0: no passwd entry for '$user'";
  }
}
笑看君怀她人 2024-08-29 08:41:52
sudo perl -F: -lane '(1..eof)?($_{$F[0]}=$_):/999999/&&($_=$_{$F[0]})&&print' /etc/passwd /etc/shadow
sudo perl -F: -lane '(1..eof)?($_{$F[0]}=$_):/999999/&&($_=$_{$F[0]})&&print' /etc/passwd /etc/shadow
燃情 2024-08-29 08:41:52

您可以使用 AnyData::Format::Password

#!/usr/bin/perl

use strict; use warnings;
use AnyData;

my $passwd = adTie(Passwd => 'passwd' );
my $shadow = adTie(Passwd => 'shadow' );

for my $user (keys %$shadow) {
    if ( $user->{fullname} and $user->{fullname} eq '999999' ) {
         print $passwd->{$user->{username}}{fullname}, "\n";
    }
}

输出:

...
Privilege-separated SSH
RPC Service User
Anonymous NFS User
HAL daemon

或:

for my $user (keys %$shadow) {
    if ( $user->{fullname} and $user->{fullname} eq '999999' ) {
        my @values = map { defined $_ ? $_ : '' }
            @{ $passwd->{$user->{username}} }{@fields};
        print join(':', @values), "\n";
    }
}

You could use AnyData::Format::Password:

#!/usr/bin/perl

use strict; use warnings;
use AnyData;

my $passwd = adTie(Passwd => 'passwd' );
my $shadow = adTie(Passwd => 'shadow' );

for my $user (keys %$shadow) {
    if ( $user->{fullname} and $user->{fullname} eq '999999' ) {
         print $passwd->{$user->{username}}{fullname}, "\n";
    }
}

Output:

...
Privilege-separated SSH
RPC Service User
Anonymous NFS User
HAL daemon

Or:

for my $user (keys %$shadow) {
    if ( $user->{fullname} and $user->{fullname} eq '999999' ) {
        my @values = map { defined $_ ? $_ : '' }
            @{ $passwd->{$user->{username}} }{@fields};
        print join(':', @values), "\n";
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文