use File::Spec; use IO::Socket; use IO::Select; use Win32::API; use Win32; use Win32::Daemon; use strict; use warnings; my $programmpfad = $0; $programmpfad =~ s/[^\\]*?$//; use Win32::Daemon::Simple Service => 'Go Back!Up', Name => 'Go Back!Up Service', Version => 'RC2.0', Info => { display => 'Go Back!Up Service', description => 'Backup Programm', user => '', pwd => '', interactive => '1', }, Params => { Tick => 0, Talkative => 0, Interval => 1, LogFile => "$programmpfad\\log\\GoBackUp.log", Description => <<'*END*',m *END* }, param_modify => { LogFile => sub {File::Spec->rel2abs($_[0])}, tAlkative => sub {undef}, }; LogNT("Running as ".SERVICEID); ServiceLoop(\&doSomething); Log("Going down"); exit; sub doSomething { my $pid = fork(); if($pid == 0) { my $server = IO::Socket::INET->new( LocalPort => 2005, type => SOCK_STREAM, Reuse => 1, Listen => 10 ) or die "Server läuft nicht: $@\n"; my $select = IO::Select->new($server); while(my @readable = $select->can_read) { foreach my $socket (@readable) { if($socket == $server) { my $client = $socket->accept; print "New client connects\n"; $select->add($client); $client->print("Welcome\n"); } else { my $computername = $socket->getline; $computername =~ s!\r?\n$!!g; # Zeilenumbruch entfernen if (defined $computername) { chomp $computername; my $WindowAvailable =""; $WindowAvailable = FindWindow("$computername"); print "$WindowAvailable\n"; if ($WindowAvailable == 0) { #$client->print ("Window ist nicht vorhanden\n"); system "start \"$computername\" cmd.exe"; $socket->print("reconnect\n"); $socket->print("port=12346\n"); #delete_socket($select, $socket); } else { $socket->print ("Window ist vorhanden\n"); $socket->print("process still running\n"); $socket->print("port=2005\n"); } } else { print "Client says: [$computername]\n"; delete_socket($select, $socket); } } } sub FindWindow { my $WindowName = shift(@_); chomp $WindowName; print "$WindowName"; my $FindWindow = new Win32::API('user32', 'FindWindow', ['P','P'], 'N');# or die print "$!"; my $name = 'Telnet 127.0.0.1'; my $class = 0; my $topHwnd = $FindWindow->Call($class, $WindowName); print "$topHwnd\n"; return $topHwnd; } sub delete_socket { my ($sel, $sock) = @_; $sel->remove($sock); $sock->close; } } } else { if (Win32::Daemon::State() == SERVICE_STOP_PENDING) { kill(0); CORE::exit; Win32::Daemon::State(SERVICE_STOPPED); Win32::Daemon::StopService(); exit 0; } } }