#!/usr/bin/perl $now = time; $DELTA = 2*60*60; @mo = (Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec); open(UTMP,'/etc/utmp') || die "can't open utmp: $!"; chdir '/dev' || die "can't cd to /dev: $!"; open(TTYS, "/etc/ttys") || die "can't open /etc/ttys: $!"; @tty = ('console'); ($tty[$.]) = /^\d*(\S+)/ while ; for ($slot = 0; read(UTMP,$utmp,36); $slot++) { $hidden = 0; ($line,$name,$host,$time) = unpack('A8 A8 A16 l', $utmp); if (!$line) { $hidden++; $line = $tty[$slot]; } else { die "$line ne $tty[$slot]" if $line ne $tty[$slot]; } ($who, $when) = (stat($line))[4,9]; if (!$who && $hidden) { next unless $when + 5*60 > $now; #print "root on $line, last touched $when, now is $now\n"; } #next if $hidden && !$who && ($when + 5*60) > $now; #next if $hidden && $when + $DELTA < $now; $time = $when if $hidden; if ($name || $hidden) { if ($hidden) { $name = &name($who); &paren($name) if $hidden; } &paren($host); ($sec,$min,$hour,$mday,$mon) = localtime($time); $date = sprintf("%s %2d %02d:%02d", $mo[$mon],$mday,$hour,$min); $date = $hidden ? "($date)" : " $date "; printf "%-10s%-8s %s %s\n", $name,$line,$date, $host; } } sub paren { $_[0] = "($_[0])" if $_[0]; } sub name { local($id) = shift; $id{$id} = (getpwuid($id))[0] unless defined $id{$id}; $id{$id}; }