[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [cobalt-users] How do i check who's logged in via CLI?
- Subject: Re: [cobalt-users] How do i check who's logged in via CLI?
- From: Per Magne Knutsen <per.knutsen@xxxxxxxxxxxxxx>
- Date: Sun Mar 9 08:41:01 2003
- Organization: Weizmann Institute
- List-id: Mailing list for users to share thoughts on Sun Cobalt products. <cobalt-users.list.cobalt.com>
>is there a way of listing all users logged in?
The command who does exactly that.
I wrote a small Perl script a while ago that will also email you a list of logged-on users. It also has a "whitelist" feature, i.e. list of users to
ignore (so your inbox doesn't get clogged by persistent users, such as yourself or other administrators who forget to log out etc). The script is
included below.
-- Per M Knutsen
#!/usr/local/bin/perl
# -------------------------------------------------------
# whosin.pl
# Emails a list of logged on users. Filters out users
# specified in the optional file .whitelist (put in
# same directory as script). Execute as cron job and
# keep a tab on who's using your machine. The script
# does not email reports when there are no users logged
# on (or they are all in .whitelist).
# -------------------------------------------------------
# Path to sendmail
my $smPath = "/usr/lib/sendmail";
# Your email address
my $email = "username\@domain.tld";
# Enable (1) for compact output
my $compact = 0;
# Load .whitelist file
open (FILE, "< .whitelist")
|| print "Cannot open .whitelist: $!\n";
my @whitelist;
while (<FILE>) { push @whitelist, $_; }
close (FILE);
# Capture who output
my @users = split(/\n/, qx/who/);
# Compose email message
my (@message, %whitelist);
foreach (@whitelist) { $whitelist{$_}++; }
foreach (@users) {
$_ =~ /^(\w*)\s*([\w\/]*)\s*([a-z]+\s+\d+)\s(\d\d:\d\d)\s*\((.*)\).*$/i;
if (!exists $whitelist{$5."\n"}) {
if ($compact) {
push @message, "$1\t @ \t$5\n";
} else {
push @message, "$1\t @ $5\t since\t $3\n";
}
}
}
# Email message
if (@message) {
open SENDMAIL, "|$smPath -t"
|| die "Cannot open sendmail: $!\n";
print SENDMAIL "To: $email\r\n";
print SENDMAIL "Subject: who update\r\n\r\n";
foreach (@message) {
print SENDMAIL $_;
}
close SENDMAIL
|| die "Cannot close sendmail: $!\n";
}