Perl列出谁在系统上
发布时间:2020-12-16 00:24:03 所属栏目:大数据 来源:网络整理
导读:???? 文件:whox.pl; 功能:列出谁在系统上,如有同一用户多次登录分组显示; 说明:列用户登录次数,使用的虚拟终端号及对应的IP; 优点:相对于”who -x”显示出的信息更加直观。??????????????????????????????????????????????????????????? ????#!/usr/b
|
????文件:whox.pl; ????#!/usr/bin/perl -w
use strict;
###################################################
# 文件:whox.pl
# 功能:列出谁在系统上,如有同一用户多次登录分组显示
# 说明:列用户登录次数,使用的虚拟终端号及对应的IP
# 优点:相对于"who -x"显示出的信息更加直观
# 版本:v0.11
# 时间:2012-5-11 12:24
# 修改:2012-6-26 15:07
# BUG1:修复无法列出通过控制台登录的用户;
# BUG2:修复printf输出tty,ip变量是undef值出错;
# 作者:半点闲
# 测试平台:
# SCO_SV scosysv 3.2 5.0.6 i386
#
# 数据结构说明:
# %whox_hash = { 存放'who -x'提取感兴趣的信息;
# user => { 以实际提取的用记名称做键;
# tty => [],# ip => [],# count => 1,同一用户在系统上的数量;
# },# }
###################################################
open (WHOX,"who -x |") or die "Can't open wordcount:$!";
my %whox_hash;
while(<WHOX>){
#############################################
#BUG1:/(^w+)s+(w+)s.*s+((?:d+.)+d+)/
#时间:2012-5-14 19:46
#说明:控制台登录的用户没有IP项导致匹配失败;
#############################################
#捕获说明:$1:用户名;$2:虚拟终端号;$3:ip
if (/(^w+)s+(w+)s.*(?: $|s+((?:d+.)+d+))/) {
if (exists $whox_hash{$1}) {
push @{$whox_hash{$1}{tty}},$2;
push @{$whox_hash{$1}{ip}},$3;
$whox_hash{$1}{count}++;
} else {
$whox_hash{$1} = {
tty => [$2],ip => [$3],count => 1,};
}
}
}
close WHOX;
#格式化输出
foreach my $user (keys %whox_hash) {
my $count = $whox_hash{$user}{count};
#同一用户多次登录系统中分组显示详细信息
if ($count > 1) {
printf "%-8s%-8s",$user,$count;
my $j = length $user;
my $k = length $count;
for (my $i = 0; $i < $count; $i++) {
######################################
#BUG2:$whox_hash{$user}{tty}[$i]等
#说明:printf输出tty,ip变量是undef值出错
#时间:2012-6-26 15:07
######################################
printf "%-10s%-20sn",$whox_hash{$user}{tty}[$i] || '(null)',$whox_hash{$user}{ip}[$i] || '(null)';
printf "%-8s%-8s"," " x ($j)," " x ($k);
}
} else {
printf "%-8s%-8s%-10s%-20sn",$count,$whox_hash{$user}{tty}[0] || '(null)',$whox_hash{$user}{ip}[0] || '(null)';
}
print "r";
}
whox.pl执行后界面:
BUG2:
BUG2修复:
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |




