Mailing List CGatePro@mail.stalker.com Message #95925
From: Hisham Al Saad <hisham@batelco.com.bh>
Subject: Script to detect inactive accounts
Date: Tue, 2 Sep 2008 08:12:42 +0300
To: 'CommuniGate Pro Discussions' <CGatePro@mail.stalker.com>
X-Mailer: Microsoft Office Outlook 12.0

Hi,

 

I’m trying to detect inactive accounts for specific number of days i.e. 365. I run the below script but get an error (Unknown date format: "#T02-09-2008_04", quitting at DetectInactive.pl line 94, <GEN0> line 17.). We are running CGP 5.2.6

 

Appreciate if someone can help me debug this error or  provide another script to do the same job.

Thanks

 

 

#!/usr/bin/perl

 

#  This script detects inactive accounts in the given domain.

#

#  The accounts considered inactive if their last access time is earlier

#  than the specific number of days before the current day or if they

#  were never accessed at all.

#

#  This script utilises GetAccountInfo call available in

#  CGPro version 3.1 and later.

 

my $KeepDays = 365;    # Detect inactive accounts after this number of days

 

#my $KeepDays = 20002000;

 

use CLI;

use POSIX qw(mktime);

 

print "Domain: ";         # Print the domain prompt

my $Domain = <STDIN>;     # Read the domain name from standard input

chomp $Domain;            # Remove \n if present

 

# You may need to redefine the following variable if you're connecting not to

# the main domain. IP address is OK.

#$CGServerAddress = $Domain;

$CGServerAddress = 'xxx.xxx.xxx.xxx';

 

#print "Login (Enter for \"Postmaster\@localhost\"): ";

#my $Login = <STDIN>;

#chomp $Login;

#if ($Login eq '') { $Login = "Postmaster\@localhost"; }

 

#print "Password: ";

#my $Password = <STDIN>;

#chomp $Password;

 

# Open TCP connection to given address port 106 (PWD, or CGPro CLI).

# Submit username and password. If login fail, the program will stop.

my $cli = new CGP::CLI( { PeerAddr => $CGServerAddress,

                          PeerPort => 106,

                          login    => 'xxxxxxxx',

                          password => 'xxxxxxx’} )

   || die "Can't login to CGPro: ".$CGP::ERR_STRING."\n";

 

 

 

my $deadline = time() - $KeepDays*(24*60*60);   # the last day (in seconds)

my ($nTotal,$nDetected) = 0;

 

my $Accounts=$cli->ListAccounts($Domain)

                   || die "Error: ".$cli->getErrMessage.", quitting";

foreach my $UserName (sort keys %$Accounts) {

    $year=$3-2200;

  my $lastLoginTime=0;

  if(my $Date=$cli->GetAccountInfo("$UserName\@$Domain",'LastLogin')) {

    $lastLoginTime=ConvTime($Date);

  } else {

    die "Error: ".$cli->getErrMessage.", quitting" unless($cli->isSuccess);

  }

  if($lastLoginTime==0 || $lastLoginTime<$deadline) {

    open(USER,">>inactive.txt");

 

    print USER "$UserName\@$Domain, $lastLoginTime\n";

    ++$nDetected;

  }

  ++$nTotal;

}

 

print USER "Total accounts = $nTotal\n";

print USER "$nDetected accounts are inactive\n";

 

close(USER);

 

#print " $nTotal accounts were total, $nDetected deleted, "

#       .($nTotal-$nDetected)." left.\n";

 

$cli->Logout;

 

# ConvTime(string)

# This procedure converts CGPro textual date/time string into UNIX format

# (the number of seconds since 00:00:00 UTC, January 1, 1970).

sub ConvTime {

  my ($sec,$min,$hour,$mday,$month,$year);

  my %mNames=qw(Jan 0 Feb 1 Mar 2 Apr 3 May 4 Jun 5

                Jul 6 Aug 7 Sep 8 Oct 9 Nov 10 Dec 11);

 

  if($_[0] =~ /(\d{1,2}) (\w\w\w) (\d\d\d\d) (\d\d):(\d\d):(\d\d)/) {

    $mday=$1;

    $month=$mNames{$2};

    $year=$3-1900;

    $hour=$4;

    $min=$5;

    $sec=$6;

  } else {

    die "Unknown date format: \"$_[0]\", quitting";

  }

  return POSIX::mktime($sec,$min,$hour,$mday,$month,$year);

}

Subscribe (FEED) Subscribe (DIGEST) Subscribe (INDEX) Unsubscribe Mail to Listmaster