LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-01-2007, 09:51 PM   #1
xlordt
Member
 
Registered: Dec 2003
Distribution: Slackware
Posts: 32

Rep: Reputation: 15
Logged in users


How do I write a shell script to give me all users that have not logged in, in a certain time?
 
Old 11-01-2007, 09:56 PM   #2
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
You can see the last login times of users with the last command. You could call that command and process the output somehow.

A more sophisticated (but ultimately probably an easier) approach is to parse the wtmp directly. There are both perl and python modules for doing this.
 
Old 11-01-2007, 10:07 PM   #3
xlordt
Member
 
Registered: Dec 2003
Distribution: Slackware
Posts: 32

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by matthewg42 View Post
You can see the last login times of users with the last command. You could call that command and process the output somehow.

A more sophisticated (but ultimately probably an easier) approach is to parse the wtmp directly. There are both perl and python modules for doing this.
Ya, I have checked out the last command, but it only allows me to input an up to date. and not a from and to date.. if you know what I mean. I guess I will have to code something.
 
Old 11-01-2007, 10:54 PM   #4
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
Yeah. I just installed the perl Sys::Utmp module, assuming it would also read wtmp, but it seems not.

The utmp/wtmp records are fixed size, so you can unpack them, but the structure differs on 64 and 32 bit systems, so you will have to cope with that.

Here's something I knocked up which you can probably modify to suit your needs:
Code:
#!/usr/bin/perl

use strict;
use warnings;
use POSIX;

open(WTMP, "</var/log/wtmp") || die "cannot open wtmp : $!\n";

my $utmp_size = 384;
my $rec;
my %last_times;
while(read_utmp_rec(\$rec))
{
        my ($typ, $pid, $line, $id, $user, $host,
                $exit_status, $session, $tsec, $tmsec, undef) =
                unpack("l l A32 A4 A32 A256 a4 l l l a", $rec);

        # we are only interested in record type 7 - logins
        next unless ($typ == 7);
        $user =~ s/\s+$//;

        if (defined($last_times{$user})) {
                if ($last_times{$user} > $tsec) {
                        $last_times{$user} = $tsec;
                }
        }
        else {
                $last_times{$user} = $tsec;
        }
}

foreach my $user (sort keys %last_times) {
        printf("last login for user %-8s was %4d mins ago: %s",
                $user,
                (time - $last_times{$user}) / 60,
                ctime($last_times{$user}));
}

sub read_utmp_rec {
        my $recref = shift;
        my $bytes_read = sysread(WTMP, $$recref, $utmp_size);
        if ($bytes_read != $utmp_size) {
                return 0;
        }
        else {
                return 1;
        }
}
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Users not logged in more than 90 days sachinh Linux - Software 2 05-21-2007 01:38 AM
4 of the same users logged on... true_atlantis Linux - Newbie 1 04-13-2006 11:54 PM
why so many logged in users? alagenchev Linux - Security 2 03-24-2005 06:19 PM
users who logged before czezz Solaris / OpenSolaris 1 11-24-2004 06:12 AM
No Users logged in X kaise_sose Linux - Software 2 10-19-2004 02:57 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:27 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration