develooper Front page | perl.beginners | Postings from April 2002

Re: Calculating Last Login on Unix

Thread Previous
From:
drieux
Date:
April 29, 2002 10:44
Subject:
Re: Calculating Last Login on Unix
Message ID:
A19B80D4-5B98-11D6-88C8-0030654D3CAE@wetware.com

On Monday, April 29, 2002, at 10:27 , gkhgkh@attbi.com wrote:

> Is there a module or a method using perl to calculate
> the last login date for a user.  I know I can do this by
> installing accounting, however I prefer not to do to
> other issues.

well there is the 'last' command that is reasonably common
to most unix boxes. The problem of course is that some
systems do not allow the wtmp file to be read by anyone
but root and members of the wheel groupd

[jeeves:~] drieux% which last
/usr/bin/last
[jeeves:~] drieux% last
last: /var/log/wtmp: Permission denied
[jeeves:~] drieux% ls -l /var/log/wtmp
-rw-r-----  1 root  wheel  8208 Apr 29 08:51 /var/log/wtmp
[jeeves:~] drieux%


so something on the order of

	my $uname = 'uname'; # use their login name

	my $cmd = "last $uname";

	open(FH, "$cmd |") or die "unable to get to last:$!\n";

	while(<FH>) {
		if (/still logged in/ ) {
			print "user $uname is currently logged in\n";
			last;
		} else {
		# the grovel and munch

		}
	}
	close(FH);

on solaris

vladimir: 56:] last -4 drieux
drieux    pts/1        jeeves           Mon Apr 29 10:36   still logged in
drieux    pts/1        jeeves           Mon Apr 29 08:33 - 08:39  (00:05)
drieux    pts/1        jeeves           Sun Apr 28 18:00 - 18:23  (00:22)
drieux    pts/1        jeeves           Sun Apr 28 11:36 - 11:36  (00:00)
vladimir: 57:]

on linux redhat 7.2:

gax: 53:] last -4 drieux
drieux   pts/1        jeeves           Mon Apr 29 09:37   still logged in
drieux   pts/1        jeeves           Mon Apr 29 09:16 - 09:35  (00:18)
drieux   pts/1        vladimir         Sun Apr 28 08:07 - 08:11  (00:03)
drieux   pts/0        jeeves           Sat Apr 27 13:55 - 13:56  (00:01)

wtmp begins Thu Apr  4 10:29:34 2002
gax: 54:]


the alternative is to do OS specific details about where
the wtmp file actually resides and then grovel it directly,
which I would not advocate - since, that makes the code
well, weird - and more work...

ciao
drieux

---


Thread Previous


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About