Example Perl Scripts

From Wiki
Revision as of 18:47, 1 February 2011 by Scott (talk | contribs) (Created page with '<source lang="perl"> #!/usr/bin/perl # A script to list which users are actually using the system right now use Text::ParseWords; my @ps_list = `ps -ef`; foreach $line (@ps_lis…')
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search
#!/usr/bin/perl
# A script to list which users are actually using the system right now

use Text::ParseWords;

my @ps_list = `ps -ef`;
foreach $line (@ps_list){
    #print($line);
    my @tokens = shellwords($line);
    my $cpu_score = $tokens[3];
    if ($cpu_score > 0){
        my $user = $tokens[0];
        if ($user ne "root"){
            print($line);
        }
    }
}
#!/usr/bin/perl -w
# return semi-colon-delimited list of non-system usernames
use Text::ParseWords;

my $output = "";
my $started = 0;
open(PW, "/etc/passwd") or die "failed to open /etc/passwd";
while (my $line = <PW>){
    chomp $line;
    my @list = quotewords(":", 1, $line);
    my $user = $list[0];
    my $id = $list[2];
    if ($id > 100){
    #print "user $user, id $id\n";
        if ($started){
            $output .= ";$user";
        } else {
            $output .= $user;
        }
    $started = 1;
    }
}
print $output;