简体   繁体   English

Perl,对散列中存储的用户使用linux命令

[英]Perl, using a linux command on users stored in a hash

Here is the code: 这是代码:

#!/usr/bin/perl

use warnings;
use strict;
use utf8;


my @temparray;
my $count = 0;
my @lastarray;
my $lastbash;


#Opens the file /etc/shadow and puts the users with an uid over 1000 but less that 65000 into an array.
open( my $passwd, "<", "/etc/passwd") or die "/etc/passwd failed to open.\n";

    while (my $lines = <$passwd>) {
        my @splitarray = split(/\:/, $lines );
        if( $splitarray[2] >= 1000 && $splitarray[2] < 65000) {

            $temparray[$count] =$splitarray[0];
            print "$temparray[$count]\n";
            $count++;
        }
    }
close $passwd;

foreach (@temparray) {
    $lastbash = qx(last $temparray);
    print "$lastbash\n";
}

What I want to do is use the built in linux command "last" on all the users stored in the @temparray. 我想做的是对所有存储在@temparray中的用户使用内置的linux命令“ last”。 And i want the output to be like this: 我希望输出是这样的:

user1:10 用户1:10

user2:22 使用者2:22

Where 22 and 10 being the number of times they logged in. How can I achieve this ? 其中22和10是他们登录的次数。我该如何实现? I have tried several different ways but I always end up with errors. 我尝试了几种不同的方法,但总是会出错。

The following should perform the task as requested: 以下应按要求执行任务:

#!/usr/bin/perl

use warnings;
use strict;
use utf8;


my @temparray;
my $count = 0;
my @lastarray;
my $lastbash;


#Opens the file /etc/shadow and puts the users with an uid over 1000 but less that 65000 into an array.
open( my $passwd, "<", "/etc/passwd") or die "/etc/passwd failed to open.\n";

    while (my $lines = <$passwd>) {
        my @splitarray = split(/\:/, $lines );
        if( $splitarray[2] >= 1000 && $splitarray[2] < 65000) {

            $temparray[$count] =$splitarray[0];
            print "$temparray[$count]\n";
            $count++;
        }
    }
close $passwd;

foreach (@temparray) {
    my @lastbash = qx(last $_); #<----Note the lines read in go to the $_ variable. Note use of my. You also read the text into array.
    print $_.":".@lastbash."\n";  #<----Note the formatting. Reading @lastbash returns the number of elements.
}

You don't really need the $count , you could just do push @temparray, $splitarray[0] . 您实际上并不需要$count ,只需执行push @temparray, $splitarray[0]

That said, I'm not sure why you need @temparray either... You can just run the command against the users as you find them. 就是说,我不确定为什么也需要@temparray ...您可以在找到用户时对他们运行命令。

my $passwd = '/etc/passwd';
open( my $fh, '<', $passwd )
  or die "Could not open file '$passwd' : $!";

my %counts;

# Get `last` counts and store them %counts
while ( my $line = <$fh> ) {
    my ( $user, $uid ) = ( split( /:/, $line ) )[ 0, 2 ];
    if ( $uid >= 1000 && $uid < 65000 ) {
        my $last = () = qx{last $user};
        $counts{$user} = $last
    }
}
close $fh;

# Sort %counts keys by value (in descending order)
for my $user ( sort { $counts{$b} <=> $counts{$a} } keys %counts ) {
    printf "%s:\t %3d\n", $user, $counts{$user};
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM