#!perl -w package PerlSvc; use strict; use warnings; my $service = 'VPNConnect'; my $logfile = 'c:/VPNConnectSVC.log'; my $configfile = 'c:/VPNConnectSVC.conf'; my $delay = 30; my @options = ( 'log=s' => \$logfile, 'config=s' => \$configfile, ); # turn on autoflush $|=1; (my $progname = $0) =~ s/.*?([^\\]+?)(\.\w+)$/$1/; our(%Config,$Verbose); sub get_options { require Getopt::Long; my @options = @_; my $usage = pop @options; $SIG{__WARN__} = sub { print "$usage\n$_[0]"; exit 1 }; Getopt::Long::GetOptions(@options); $SIG{__WARN__} = 'DEFAULT'; } sub configure { %Config = (ServiceName => "$service", DisplayName => "$service", Parameters => "--log $logfile --config $configfile", Description => "Automatically connect to given VPN Gateway"); } sub Startup { get_options(@options, <<__USAGE__); Try `$progname --help` to get a list of valid options. __USAGE__ Log("\n$Config{DisplayName} starting at: ".localtime); open FH, "<$configfile"; my @CONF=; close FH; while (ContinueRun($delay)) { foreach my $line (@CONF) { chomp; next if ($line =~ /^#/); my ($DIR,$PROFILE,$USR,$PWD)=split /\|/,$line; chdir("$DIR"); `vpnclient.exe stat>c:\\vpnconnect.log`; my $est=1; open RETF, "; close RETF; foreach (@ret) { if ($_ =~ /No connection exists/) { $est=0; } } if ($est==0) { Log("Versuche Verbindungsaufbau"); `vpnclient.exe connect $PROFILE user $USR pwd $PWD>vpnconnect.log`; } } } Log("$Config{DisplayName} stopped at: ".localtime); } sub Log { my $msg = shift; open(my $f, ">>$logfile") or die $!; print $f "$msg\n"; close $f; } sub Install { #get_options('name=s' => \$service, @options, <<__USAGE__); get_options(@options, <<__INSTALL__); Valid --install suboptions are: auto automatically start service --log log file name [$logfile] --config config file name [$configfile] For example: $progname --install auto --log logfile --config configfile __INSTALL__ configure(); } sub Help { print <<__HELP__; Automatically connects to VPN Gateway given in configfile Install it as a service: $progname --install auto --log logfile --config configfile net start $service You can pause and resume the service with: net pause $service net continue $service To remove the service from your system, stop und uninstall it: net stop $service $progname --remove logfile defaults to c:\\VPNConnectSVC.log configfile defaults to c:\\VPNConnectSVC.conf __HELP__ # Don't display standard PerlSvc help text $Verbose = 0; } sub Pause { Log("$Config{ServiceName} is about to pause at ".localtime); } sub Continue { Log("$Config{ServiceName} is continuing at ".localtime); } sub Remove { get_options('service=s' => \$service, <<__REMOVE__); No valid --remove suboptions For example: $progname --remove __REMOVE__ $Config{ServiceName} = "$service"; $Config{DisplayName} = "$service"; }