Summary: Inactive users

From: arun sanghvi <sanghvi_at_proto.wilm.ge.com>
Date: Mon, 28 Sep 1998 20:51:39 -0400

Hello,

In response to my question about how to identify "inactive" users
I received several responses suggesting to parse the output from
"finger" and "last".

I also received a "C" program as shown below.

usage:

inactive 90 {will list all users who have not logged in 90 days.

/* List users whose last login was at least as many days ago as the
   specified argument. */

#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <pwd.h>
#include <lastlog.h>

#define LASTLOGS "/var/adm/lastlog"
#define SECPERDAY 86400

void main(int ac, char **av)
{
  static FILE *logs;
  static time_t cutoff, earliest = 0;
  static struct passwd *pwd;
  static struct lastlog log;
  static long log_offset;
  static char *asctim, strtime[12];

  if (ac != 2) {
    fprintf(stderr,"usage: inactive number-of-days\n");
    exit(2);
  }

  if (!( logs = fopen(LASTLOGS,"r") )) {
    perror(LASTLOGS);
    exit(2);
  }

  cutoff = time(0) - abs(atoi(av[1]))*SECPERDAY;

  for (setpwent(); pwd = getpwent(); ) {
    if (!strcmp(pwd->pw_passwd,"*") || !strcmp(pwd->pw_passwd,"Nologin"))
      continue;
    log_offset = pwd->pw_uid * sizeof(log);
    if (fseek(logs,log_offset,SEEK_SET)) {
      perror("fseek lastlogs");
      exit(2);
    }
    if (fread(&log,sizeof(log),1,logs) != 1) continue;
    if (log.ll_time > 0 && (log.ll_time < earliest || earliest == 0))
      earliest = log.ll_time;
    if (log.ll_time >= cutoff) continue;
    if (log.ll_time == 0)
      strcpy(strtime,"never");
    else {
      asctim = ctime(&log.ll_time);
      sprintf(strtime,"%.2s-%.3s-%.4s",asctim+8,asctim+4,asctim+20);
    }
    printf("%-8s %-30.30s %s\n",pwd->pw_name,pwd->pw_gecos, strtime);
  }
  endpwent();
  if (earliest > 0) {
    asctim = ctime(&earliest);
    printf("\nRecords go back to %.2s-%.3s-%.4s\n", asctim+8,
      asctim+4, asctim+20);

Arun Sanghvi
GENE
Received on Tue Sep 29 1998 - 00:52:47 NZST

This archive was generated by hypermail 2.4.0 : Wed Nov 08 2023 - 11:53:38 NZDT