open LASTLOG, "/var/log/lastlog" or die; # location may vary my $struct_lastlog = "L a8 a256"; # see "man lastlog" for struct sizes my $size = length pack $struct_lastlog, 0, "", ""; while (my($user, $pwd, $uid) = getpwent) { next unless seek LASTLOG, $size * $uid, 0; next unless read(LASTLOG, my $buf, $size) == $size; my ($time, $line, $host) = unpack $struct_lastlog, $buf; s/\0*$// for $line, $host; # trim NULs next unless $time; printf "%8s %5d %30s %8s %s\n", $user, $uid, scalar localtime $time, $line, $host; }