> Tech > Listing 2

Listing 2

Tech - Par Renaud ROSSET - Publié le 24 juin 2010
email

Extrait de TheftDetector.pl

use Mail::Sendmail;
use Net::Ping;
use strict;
system ("title Theft detection script");
system ("color 1F");

# Filename: TheftDetector.pl
# Purpose: To test Ping responsiveness
# Uses two input files:
# TestObjects.txt contains all the PC or server targets for testing.
# PagerRecipients.txt lists the page recipients email addresses.

Listing 2

# Run the script as a scheduled task from a machine
# that wont be shut down.

# Enter the SMTP server that will send pager messages.
$main::SMTP_Server= »mail.yourcompany.com »;

# Enter the name of the sender of pager messages.
$main::Sender = _fredsmith@yourcompany.com ;

# Configure the location of TestObjects.txt and PagerRecipients.txt.
$main::pathtoobjectslist= »D:\\Scripts\\TestObjects.txt »;
$main::pathtopagerlist= »D:\\Scripts\\PagerRecipients.txt »;

# Configure the folder location of log files.
$main::pathtologfile= »D:\\Scripts\\Logs »;

# Create the folder for the log files if it doesn t already exist.
if ( ! -e $main::pathtologfile) {
print « $main::pathtologfile does not exist attempting creation\n »;
mkdir ($main::pathtologfile, 0777) || die « Can t create
log file folder: $! »;
}

# Get the date and time for log filename.
$main::datetime =localtime(time);
$main::datetime =~ s/ / /g;
$main::datetime =~ s/:/./g;

# Append the default log filename to the date and time
# stamped name from above.
$main::pathtologfile= « $main::pathtologfile\\$main::datetime FailureLog.txt »;

# Get the names of the page recipients
# in case an error condition occurs later.
open(OBJECTFILE, »<$main::pathtoobjectslist") || die "Unable to open test objects file!"; my @Object_array = ;
close(OBJECTFILE) || die « close0: $! »;
%main::server;
@main::pinglist;
open(PAGERFILE, »<$main::pathtopagerlist") || die "Unable to open pager recipient list file!"; while () {
# Check for blank lines.
/\S/ or next;
# Remove any new line characters.
chomp($_);
push (@main::PagerRecipients, « $_ »);
}
close(PAGERFILE) || die « close0: $! »;
print « Recipients:\n@main::PagerRecipients\n »;

# Run the Initialize subroutine below to get a list of nodes for the test.
&Initialize(@main::PagerRecipients);

# The Do While statement keeps this running as an endless loop.
do {
print « \n*************** TESTING ***************\n »;
&TestObjects;
# &TestObjects(my @pinglist);
# NOTE: If you re going to use this script in conventional
# responsiveness testing mode, you can comment out the next 8 lines
# and uncomment the 9th and 10th lines. This would have the script loop
# every 30 seconds without entering heightened responsiveness mode.
if (keys(%main::server) > 0) {
print « Entering Heightened Alert State.\n »;
print « ************* SLEEPING **************\n »;
sleep (10);
} else {
print « ************* SLEEPING **************\n »;
sleep (30);
}
# print « *************** SLEEPING **************\n »;
# sleep (30);
} while ( $main::pathtoobjectslist );

########################################################
sub TestObjects # Pings nodes.
########################################################
{
my ($Risk);
# Create the Ping object.
my $p = Net::Ping->new(« icmp »);
# Open the failure log file and append.
open(LOGFILE, « >>$main::pathtologfile ») || die « Unable to open log
file! »;
foreach my $item(@main::pinglist) {
# Remove any new line characters.
chomp($item);
# Split info into the host name and location.
my ($host, $location) = split(/,/, $item);
# Ping the host.
if ($p->ping($host, 5)) {
# If the node failed last time, it s in the %server list.
# Delete the entry in the list because it s back online.
# Send the back online message.
#my (%server);
if ( $main::server{$host} ) {
delete $main::server{$host};
&Page(« $host @ $location !PC Back Online! »,
« All Clear Message »);
} else {
print « $host Ping OKAY on try #1\n »;
}
} else {
print « $host ping failed on try #1 going for #2\n »;
sleep (1);
if ($p->ping($host, 5)) {
if ( $main::server{$host} ) {
delete $main::server{$host};
&Page(« $host @ $location !PC Back Online! »,
« All Clear Message »);
} else {
print « $host Ping OKAY on try #2\n »;
}
} else {
print « $host ping failed on try #1 and #2\n »;
# If the node failed last time, it s in the %server list.
# If it s not in the list, it was online during the last
# test and a page now must be sent because this is a new
# problem.
# Send the offline message.
if ( ! $main::server{$host} ) {
$main::server{$host} = « off »;
# Testing to see how many computers are off and
# assign theft risk.
# NOTE: If you are going to use this script in a
# conventional responsiveness testing mode, you can
# comment out the next 10 lines and uncomment line 11.
if ( keys(%main::server) == 1) {
$Risk = « Low »;
}
if ( keys(%main::server) == 2) {
$Risk = « Medium »;
}
if ( keys(%main::server) > 2 ) {
$Risk = « High »;
}
&Page(« $host @ $location !Possible theft in progress!
Risk $Risk », « Warning Message »);
# &Page(« $host @ $location
# !Offline! », « Warning Message »);
} else {
print « $host page skipped due to previous failure\n »;
}
}
}
}
close(LOGFILE) || die « close0: $! »;
$p->close();
} # End TestObjects sub

########################################################
sub Page # Sends emails and pages via email.
########################################################
{
my ($Message, $Subject) = @_;
# Add the time onto the pager message.
$Message = « $Message « .localtime(time);
# Send a mail (pager) message.
my %mail;
$mail{From} = « $main::Sender »;
$mail{To} = « @main::PagerRecipients »;
$mail{Smtp} = $main::SMTP_Server;
$mail{Subject} = « $Subject »;
$mail{Message} = « $Message »;
sendmail(%mail) or warn « Error sending message:\n$Sendmail::error »;
# Uncomment this if you want to see Sendmail log.
# print « Log says:\n », $Mail::Sendmail::log;
print « $Message\n »;
print LOGFILE « $Message \n »;
# You can send a net send message to this computer or another
# computer if you like. Examples:
# system (« net send %computername% $Message »);
# system (« net send server5 $Message »);
} # End Page sub

##########################################################
sub Initialize # Determines what nodes are on and are
# targeted for the current detection run.
##########################################################
{
my $counteron = 0;
my $counteroff = 0;
my ($conunits,$coffunits);
# Create the Ping object.
my $p = Net::Ping->new(« icmp »);
# Open the log file to add list of computers that will be tested.
open(LOGFILE, « >>$main::pathtologfile ») || die « Unable to open log file! »;
# Print header line to log file.
print LOGFILE « ************** « .localtime(time). » ****************\n »;
# Print pager recipients to log file.
print LOGFILE « Pager recipients:\n@main::PagerRecipients\n\n »;
# Open the Test object file and load array with these objects.
open(OBJECTFILE, »<$main::pathtoobjectslist") || die "Unable to open test objects file!"; my @Object_array = ;
close(OBJECTFILE) || die « close0: $! »;
# Remove any new line characters.
chomp(@Object_array);
foreach my $Object (@Object_array) {
# Check for blank lines
$Object =~ /\S/ or next;
# Split the info into the host name and location.
my ($host, $location) = split(/,/, $Object);
# Ping the host.
if ($p->ping($host, 5)) {
# Increment the counter for on devices by 1.
$counteron++;
# Print to the screen and the log file the name and location.
print « $host,$location online\n »;
print LOGFILE « $host,$location online\n »;
# Add the node to the array that s the ping target list.
push (@main::pinglist, « $host,$location »);
} else {
# PC is off.
# Increment the counter for off devices by 1.
$counteroff++;
# Print to the screen and the log file the name and location.
print « $host,$location offline-excluded from test\n »;
print LOGFILE « $host,$location offline-excluded from test\n »;
}
}
# If the counter for the on or off nodes is = 1, use the word « unit » in
# the screen and log file output. If it s anything else, use « units ».
if ( $counteron == 1 ) {
$conunits = « unit »;
} else {
$conunits = « units »
}
if ( $counteroff == 1 ) {
$coffunits = « unit »;
} else {
$coffunits = « units »
}
# Print to screen and log file the on and off counter info.
print « \n$counteron $conunits on & $counteroff $coffunits off\n »;
print LOGFILE « \n$counteron $conunits on & $counteroff $coffunits off\n »;
# Close open files.
close(LOGFILE) || die « close0: $! »;
# Close Ping object.
$p->close();
} # End Initialize sub

Téléchargez cette ressource

Guide inmac wstore pour l’équipement IT de l’entreprise

Guide inmac wstore pour l’équipement IT de l’entreprise

Découvrez toutes nos actualités à travers des interviews, avis, conseils d'experts, témoignages clients, ainsi que les dernières tendances et solutions IT autour de nos 4 univers produits : Poste de travail, Affichage et Collaboration, Impression et Infrastructure.

Tech - Par Renaud ROSSET - Publié le 24 juin 2010