Perl: list who is on the system

Source: Internet
Author: User

File: whox. pl;
Function: lists who is on the system. If the same user has logged on to the group multiple times;
Description: lists the user logon times, virtual terminal numbers used, and corresponding IP addresses;
Advantage: it is more intuitive than the information displayed by "who-X.

#! /Usr/bin/perl-wuse strict; ######################################## ########## file: whox. PL # function: lists who is on the system. If multiple logon groups are displayed for the same user, # Description: lists the user logon times, virtual terminal numbers, and corresponding IP addresses. # advantages: compared with the information displayed in "who-X", it is more intuitive # version: v0.11 # Time: # modification: # bug1: fixed the problem of listing users logging on through the console; # bug2: fixed the output tty of printf. An error occurred while the IP variable was an UNDEF value. # Author: semi-idle # Test Platform: # sco_sv scosysv 3.2 5.0.6 i386 # Data Structure Description: # % whox_hash = {stores 'Who-x' to extract information of interest; # user => {uses the actual extracted name as the key; # tty => [], # IP => [], # Count => 1, number of users on the same system ;#}, #}###################################### ############ open (whox, "Who-x |") or die "can't open wordcount: $! "; My % whox_hash; while (<whox>) {####################################### ####### bug1: /(^ \ W +) \ s + (\ W +) \ s. * \ s + ((?: \ D ++ \.) + \ D +)/# Time: # Note: if the user logging on to the console does not have an IP address, the matching fails; ######################################## ###### capture description: $1: User Name; $2: virtual terminal number; $3: ipif (/(^ \ 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; # formatting the output foreach my $ user (Keys % whox_hash) {my $ COUNT = $ whox_hash {$ user} {count }; # If ($ count> 1) {printf "%-8 S %-8 s", $ user, $ count; my $ J = length $ user; my $ k = length $ count; For (my $ I = 0; $ I <$ count; $ I ++) {####################################### bug2: $ whox_hash {$ user} {tty} [$ I] and so on # Description: printf outputs tty, IP variable is UNDEF value Error # Time: ###################################### printf "%-10 S %-20s \ n ", $ whox_hash {$ user} {tty} [$ I] | '(null)', $ whox_hash {$ user} {IP} [$ I] | '(null) '; printf "%-8 S %-8 s", "" X ($ J), "" X ($ K );}} else {printf "%-8 S %-8 S %-10 S %-20s \ n", $ user, $ count, $ whox_hash {$ user} {tty} [0] | '(null)', $ whox_hash {$ user} {IP} [0] | '(null) ';} print "\ r ";}

The output interface of the original 'Who-x' command:

Page after whox. pl execution:

Bug2:

Bug2 repair:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.