Here is the whole of it. Basically what it does is parses a file made by a login script each time a user logs in and matches it up with data from a a patchlink server (patchlink.com) running SQL server. It parses the data from these two sources and nags the users about PCs which have not logged onto our domain in 60 days and have also made no contact with the patchlink server. Its Kludgey - but its working, now Im working on cleaning up the code...
# Script: OldAgents.pl
# Find all old Patchlink Agents
# TFiedler
# We are dependant on these...
use Win32::ODBC;
use Win32::NetResource;
use Win32::WinError;
use POSIX qw(strftime);
use Net::SMTP;
# Connect to the udrive
my %NetResource = (
LocalName => "U:",
RemoteName => "\\\\SERVER\\share"
);
my $User = "DOMAIN\\USER";
my $Password = "PASSWORD";
Win32::NetResource::AddConnection( \%NetResource, $Password, $User, 1
+);
# If you are having problems connecting uncomment the following few li
+nes and comment
# out the line above...
#if( Win32::NetResource::AddConnection( \%NetResource, $Password, $Use
+r, 1 ) )
#{
# print "Successful\n";
#}
#else
#{
# print NetError();
#}
# End connection to udrive routine...
if ( -f "oldagents.txt" )
{
print "Deleting old data file\n";
unlink("oldagents.txt");
print "Continuing...\n";
}
else
{
print "Data file does not exist.\nContinuing...\n";
}
open (OLDAGENTS,">oldagents.txt") || die "unable to open file c:\worki
+ng\oldagents.txt\n";
# Lets calculate 60 days ago from NOW!
# We are doing this in seconds.
# The next 3 lines of code were gotten
# from Perlmonks.org...
my $thisday = time;
my $SixtyDaysPrevious = $thisday - 60*24*60*60;
$SixtyDaysPrevious = strftime "%m/%d/%Y", (localtime($SixtyDaysPreviou
+s));
# End of perlmonks code.
my $db = new Win32::ODBC( "DSN=patchlink_PLUS" );
# does this DB actually exist?
if ( ! $db )
{
die "Error connecting: " . Win32::ODBC::Error() . "\n";
}
# YES! get the data...
if ( $db->Sql ( "SELECT AgentName, LastContactDate, AgentVersion from
+PLUS.dbo.UP_Agents
where LastContactDate < '$SixtyDaysPrevious'
order by LastContactDate" ))
{
# Ah you screwed UP!
print "Error submitting SQL statement: " . $db->Error() . "\n";
}
else
{
# Show me the money
use Time::localtime;
while( $db->FetchRow() )
{
my @DATA=$db->Data ("AgentName", "LastContactDate", "AgentVers
+ion");
my (undef, undef, $agent) = split(/\\/, $DATA[0]);
# if you think you are getting bad data uncomment the
# line below and watch the output. You should see
# pcnames one per line w/o the \\ ie FIEDLER not \\FIEDLER
# print "$agent\n";
my $file="u:\\usr\\NAV\\OS\\$agent\.txt";
# Added 12/07/04 (TF)
if ( -f $file ) {
open (PCFILE, "$file") || warn "Unable to open $agent.txt\
+n";
while (my @line = <PCFILE>) {
foreach my $info(@line) {
my (undef, $login, $os, $sp, $lc, $ie, $ip, undef)
+ = split(",",$info);
my $modtime = (stat($file))[9];
$modtime = $modtime - 86400;
if ( $modtime == "" )
{
$modtime = "null";
}
else
{
$modtime = POSIX::strftime ("%B %d",$modtime,0
+,0,0,0,0,0,0,0);
#$modtime = POSIX::strftime ("%D",$modtime,0,0
+,0,0,0,0,0,0);
}
printf OLDAGENTS "Agent: %s\nAgent Version: %s\nLa
+st Contact Date: %s\nUser: %s\nLast login: %s\nLast IP: %s\n\n",
$DATA[0], $DATA[2], $DATA[1], $login, $modtime, $i
+p;
# NOW Lets NAG the user...
open (LUSER, ">tmp/$agent.txt") || die "unable to
+create $agent.txt\n";
printf LUSER "Agent: %s\nAgent Version: %s\nLast C
+ontact Date: %s\nUser: %s\nLast in: %s\nLast IP: %s\n\n",
$DATA[0], $DATA[2], $DATA[1], $login, $modtime, $i
+p;
close (LUSER);
open (LUSER, "tmp/$agent.txt") || die "unable to o
+pen $agent.txt\n";
$/=undef; # Sluuuuurp...
while (my @EMAIL=<LUSER>) {
$smtp=Net::SMTP->new('smtpserver.domain.com');
$smtp->mail( $ENV{USER} );
#$smtp->to('USER\@domain.com');# For testing
$smtp->to("$login\@domain.com");
#$smtp->cc('USER2\@domain.com');
#$smtp->bcc('USER3\@domain.com');
$smtp->data();
$smtp->datasend("FROM: help\@domain.com\n");
$smtp->datasend("SUBJECT: PC name $agent\n");
$smtp->datasend("Below is a record of a PC tha
+t was last logged into by you. This PC has not been\n");
$smtp->datasend("logged into the domain since
+$modtime. If you use this PC occasionally,\n");
$smtp->datasend("please make sure that it is t
+urned on for at least four hours per month. If you no\n");
$smtp->datasend("longer are in posession of th
+is PC, please contact the help desk with the information\n");
$smtp->datasend("below. If you use this PC on
+a regular basis please contact the help desk with the\n");
$smtp->datasend("information below.\n\nThank Y
+ou\n\n");
foreach my $line(@EMAIL) {
$smtp->datasend($line);
}
$smtp->dataend();
$smtp->quit;
}
#unlink ("tmp/$agent.txt");
}
}
}
else {
my $modtime = (stat($file))[9];
$modtime = $modtime - 86400;
if ( $modtime == "" )
{
$modtime = "null";
}
else
{
$modtime = POSIX::strftime ("%B %d",$modtime,0,0,0,0,0
+,0,0,0);
#$modtime = POSIX::strftime ("%F",$modtime,0,0,0,0,0,0
+,0,0);
}
printf OLDAGENTS "PC: %s\nAgent Version: %s\nLast Patchlin
+k contact: %s\nLast domain login: %s\n\n",
$DATA[0], $DATA[2], $DATA[1], $modtime;
}
}
}
close(OLDAGENTS);
open (OLDAGENTS, "oldagents.txt") || die "unable to open oldagents.txt
+\n";
$/=undef; # Sluuuuurp...
while (@OLDA=<OLDAGENTS>) {
$smtp=Net::SMTP->new('smtp.domain.com');
$smtp->mail( $ENV{USER} );
$smtp->to('USER1\@domain.com');
$smtp->cc('USER2\@domain.com');
$smtp->bcc('USER3\@domain.com');
$smtp->data();
$smtp->datasend("FROM: Patchlink\@Patchlink.domain.com\n");
$smtp->datasend("SUBJECT: Expired patchlink agents\n");
$smtp->datasend("Patchlink agents 60 days old or older:\n\n\n");
foreach $line(@OLDA) {
$smtp->datasend($line);
}
$smtp->dataend();
$smtp->quit;
}
# Clean up...
close(OLDAGENTS); # close open files
$db->Close(); # close the DB
Win32::NetResource::CancelConnection( "U:", 1, 1); # close the udrive!
+!!
# Subs
sub NetError
{
my( $Error, $Text, $Provider );
$Error = Win32::GetLastError();
if( ERROR_EXTENDED_ERROR == $Error )
{
Win32::NetResource::WNetGetLastError( $Error, $Text, $Provider
+ );
$Result = "Error: $Error: $Text (Genereated by $Provider)";
}
else
{
$Text=Win32::FormatMessage( $Error );
$Result="Error: $Error: $Text";
}
return $Result
}
|