Linux login / logout log to limit computer

I would like to know how to register the login and logout of a user: it would be difficult to measure how long someone was connected in a month.

I know the "last" command can be used. But this command is based on a file that has r / w permission for the user, hence the ability to modify this data. I would like to register this data within two months.

Why would I do this? In fact, I would like to prevent regular users from using the computer (mostly graphics mode) for more than an hour a day - except weekends and 10 hours in total per week.

Cedric

(System used: kubuntu, / Programming language: bash script)

+2


a source to share


2 answers


Not exactly what you're looking for, but this article shows you how to restrict a user to only be able to log in for a specified time.



0


a source


Here's a Perl script that summarizes the content printed last

. It is based on an example from the book Running Linux , cleaned up for readability, and fixed to work on a modern machine (the last's

output format seems to have changed since the original was written). Save the code in a file and you can run it by outputting last

to it.



#!/usr/bin/perl 

# logintime.pl - Summarise amount of time a user is logged in.
# Usage: last | perl logintime.pl

use strict;
use warnings;

my %hours;
my %minutes;
my %logins;

# While we have input...
while ( <> ) {

  # Extract the username and login time...
  if ( my ($username, $hrs, $mins) = /^(\S+).*\((\d+):(\d+)\)/ ) {
    # Increment total hours, minutes, and logins 
    $hours{$username}   += $hrs; 
    $minutes{$username} += $mins; 
    $logins{$username}++;
  } 
} 

# For each unique user...
foreach my $user ( sort keys %hours ) { 
   # Calculate the total hours and minutes...
   $hours{$user}   += int($minutes{$user} / 60); 
   $minutes{$user} %= 60;

   # Print the information for this user...
   print "User $user, total login time "; 
   printf "%02d:%02d, ", $hours{$user}, $minutes{$user}; 
   print "total logins $logins{$user}.\n"; 
}

      

+1


a source







All Articles