You may want to try the latest version on our PPM site. It includes
callbacks. This makes your code way easier. Have a look at
http://www.roth.net/perl/daemon/ for details. Sample code (from the web
page):
use Win32::Daemon;
Win32::Daemon::RegisterCallbacks( {
start => \&Callback_Start,
running => \&Callback_Running,
stop => \&Callback_Stop,
pause => \&Callback_Pause,
continue => \&Callback_Continue,
} );
%Context = (
last_state => SERVICE_STOPPED,
start_time => time(),
);
sub Callback_Running
{
my( $Event, $Context ) = @_;
# Note that here you want to check that the state
# is indeed SERVICE_RUNNING. Even though the Running
# callback is called it could have done so before
# calling the "Start" callback.
if( SERVICE_RUNNING == Win32::Daemon::State() )
{
# ... process your main stuff here...
# ... note that here there is no need to
# change the state
}
}
sub Callback_Start
{
my( $Event, $Context ) = @_;
# Initialization code
# ...do whatever you need to do to start...
$Context->{last_state} = SERVICE_RUNNING;
Win32::Daemon::State( SERVICE_RUNNING );
}
sub Callback_Pause
{
my( $Event, $Context ) = @_;
$Context->{last_state} = SERVICE_PAUSED;
Win32::Daemon::State( SERVICE_PAUSED );
}
sub Callback_Continue
{
my( $Event, $Context ) = @_;
$Context->{last_state} = SERVICE_RUNNING;
Win32::Daemon::State( SERVICE_RUNNING );
}
sub Callback_Stop
{
my( $Event, $Context ) = @_;
$Context->{last_state} = SERVICE_STOPPED;
Win32::Daemon::State( SERVICE_STOPPED );
# We need to notify the Daemon that we want to stop callbacks
and the service.
Win32::Daemon::StopService();
}
-----Original Message-----
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of
$Bill Luebkert
Sent: Friday, October 24, 2003 5:59 PM
To: Raymond Forbes
Cc: [EMAIL PROTECTED]
Subject: Re: Win32::Service problems.
Raymond Forbes wrote:
> I am trying to write a script to go through a class c network and find
all
> instances of a virus. Below is my code. The problem I am
experiencing is I
> am not getting back accurate information. My best guess is
%service_list
> and %status need to be initialized because they seem tobe "holding on"
to
> the previous loops information. Anybody have any ideas?
>
> For a bit more information. Everytime this script succesfully
connects to a
> remote machine it responds that it found the virus and the service is
turned
> off. Now this is true for the FIRST machine it runs across, but not
the
> others.
>
> Oh, I am using perl 5.8.0
Try my re-formatted version (I think it's important to define your
vrbls where they are needed rather than sticking them all up top).
I switched to icmp for the ping.
use strict;
use Win32;
use Net::Ping;
use Win32::Service;
my %state = (0 => 'unknown', 1 => 'stopped', 2 => 'starting', 3 =>
'stopping',
4 => 'running', 5 => 'resuming', 6 => 'pausing', 7 => 'paused');
my $network = '10.34.9';
my $display_name = 'WINS Client';
my $p = Net::Ping->new('icmp', 5);
print "Populating server list\n";
my @addresses;
for (my $i = 1; $i < 10; $i++) {
my $test_address = "$network.$i";
print "ping ($test_address)\n";
if ($p->ping ($test_address)) {
push @addresses, $test_address;
print "$test_address added\n";
}
}
print "Starting Virus Check\n";
foreach (@addresses) {
print "Starting Check of $machine\n";
my $machine = "\\\\$_";
my %service_list;
if (Win32::Service::GetServices ($machine, \%service_list)) {
print "Succesfully Connected\n";
} else {
print "Could not Connect\n";
next;
}
if (defined $service_list{$display_name}) {
print "virus found, the service $display_name is on the
" .
"machine\n checking status\n";
my %status;
Win32::Service::GetStatus ($machine,
$service_list{$display_name}, \%status);
print "The virus is $state{$status{CurrentState}}\n";
} else {
print "This machine does not have the Virus\n";
}
}
__END__
--
,-/- __ _ _ $Bill Luebkert
Mailto:[EMAIL PROTECTED]
(_/ / ) // // DBE Collectibles Mailto:[EMAIL PROTECTED]
/ ) /--< o // // Castle of Medieval Myth & Magic
http://www.todbe.com/
-/-' /___/_<_</_</_ http://dbecoll.tripod.com/ (My Perl/Lakers stuff)
_______________________________________________
Perl-Win32-Admin mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
_______________________________________________
Perl-Win32-Admin mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs