简体   繁体   English

使用perl脚本的唯一的已连接Linux用户

[英]unique connected linux users with perl script

I need to know, at any given time, how many unique users are connected to a redhat server. 我需要在任何给定时间知道有多少唯一用户连接到redhat服务器。 The following commands accomplish this easily: 以下命令可以轻松完成此操作:

who | awk '{ print $1 }' | sort -ud | wc -l

However, I need this functionality within a perl script, so that a network monitoring utility can run it at scheduled times, and track the number of unique connections over time. 但是,我需要在perl脚本中使用此功能,以便网络监视实用程序可以在计划的时间运行它,并跟踪一段时间内唯一连接的数量。

While I would like to learn scripting with perl, I have no idea how long it would take to learn to script this myself, and I don't have enough time to dedicate to learning perl at the moment. 虽然我想学习使用Perl编写脚本,但我不知道自己学习脚本需要花费多长时间,而且我目前没有足够的时间专门用于学习Perl。 Any assistance in creating a perl script with the above functionality would be greatly appreciated. 在使用上述功能创建Perl脚本方面的任何帮助将不胜感激。

To easily wrap this into a perl script, do this: 要将其轻松包装到perl脚本中,请执行以下操作:

#!/usr/bin/perl

$result = `who | awk '{ print \$1 }' | sort -ud | wc -l`;
print $result;

Be sure to escape $1, or else it will be interpolated by Perl. 请确保转义$ 1,否则它将由Perl内插。

What you do with $result after that depends on what exactly you're trying to do. 之后,您对$ result的处理方式取决于您要尝试执行的操作。 You could average it over time, store every result in a file or database...it really depends on what you want to do with the result once you have it. 您可以将其随时间平均,将每个结果存储在文件或数据库中……这实际上取决于您对结果的处理方式。 If you'd care to provide more details I can offer some advice. 如果您想提供更多详细信息,我可以提供一些建议。

Assuming that you want a Perl subroutine that will return the number of users, you could do this: 假设您想要一个Perl子例程来返回用户数,则可以执行以下操作:

 sub uniqueUsers {
    my %users;
    foreach my $line (split("\n", `who`)) {
        $users{(split(" ", $line))[0]}++;
    };
    return scalar(keys(%users));
 }

If you want a complete standalone Perl script that will just print out the total number of unique users and exit, add 如果您想要一个完整的独立Perl脚本,该脚本仅打印出唯一用户总数并退出,请添加

#!/usr/bin/perl

as a line at the beginning, and 作为开头的一行

print uniqueUsers() . "\n";

as a final line and you're done. 作为最后一行,您就完成了。 Oh, and make the file executable if you want to run it standalone :-) 哦,如果要独立运行该文件,请使其可执行文件:-)

To get the count: 获得计数:

my $usercount = `who | awk '{print $1}' | sort | uniq | wc -l`;

To get the actual usernames into an array: 要将实际的用户名放入数组中:

my @users = `who | awk '{print \$1}' | sort | uniq`;

It's worth mentioning that learning perl basics is fairly quick (and for me, fun). 值得一提的是,学习perl基础知识相当快(对我来说很有趣)。 I recommend picking up a copy of Learning Perl from O'Reilly Media. 我建议从O'Reilly Media那里获取Learning Perl的副本。 It's available in both paperback and in ebook format, and it is the best text book i've ever read for learning anything. 它提供平装本和电子书两种格式,并且是我曾经阅读过的关于学习任何内容的最佳教科书。 Grab that one, and it won't take you long to get the basics down, despite being short on time. 抓住那一个,尽管时间很短,但花很长时间您就可以掌握基础知识。

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

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