DHCP und Squid-Initscripts optimiert fuer ein schnelleres starten und stoppen.
[people/pmueller/ipfire-2.x.git] / src / scripts / vpn-watch
CommitLineData
dced81b2 1#!/usr/bin/perl
c6c9630e 2##################################################
ad60e3ea 3##### VPN-Watch.pl Version 0.3 #####
c6c9630e
MT
4##################################################
5
dced81b2 6use strict;
c6c9630e 7
dced81b2
CS
8require '/var/ipfire/general-functions.pl';
9my @vpnsettings;
10my $i = 0;
11my $file = "/var/run/vpn-watch.pid";
ad60e3ea
CS
12my $debug = 0;
13
14if ( -e $file ){logger("There my be another vpn-watch runnning $file exists, vpn-watch will not start.");exit 1;}
15if ($debug){logger("Call of vpn-watch pid not is not existing.");}
c6c9630e 16
dced81b2 17system("echo $$ > $file");
c6c9630e 18
dced81b2 19while ( $i == 0){
ad60e3ea
CS
20 sleep(300);
21 if ($debug){logger("We will wait 300 seconds before next action.");}
22
dced81b2
CS
23 if (open(FILE, "${General::swroot}/vpn/config")) {
24 @vpnsettings = <FILE>;
25 close(FILE);
26 unless(@vpnsettings) {exit 1;}
27 }
28
29foreach (@vpnsettings){
30 my @settings = split(/,/,$_);
31
32 if ($settings[27] ne 'RED'){next;}
33 if ($settings[4] ne 'net'){next;}
34 if ($settings[1] ne 'on'){next;}chomp($settings[29]);
35 if ($settings[29] ne 'on'){next;}
36
37 my $remotehostname = $settings[11];
ad60e3ea
CS
38
39 if ($debug){logger("Checking connection to $remotehostname.");}
40
dced81b2
CS
41 my $remoteip = `/usr/bin/ping -c 1 $remotehostname 2>/dev/null | head -n1 | awk '{print \$3}' | tr -d '()' | tr -d ':'`;chomp($remoteip);
42 if ($remoteip eq ""){next;}
43 if (system("ipsec whack --status | grep $remoteip >/dev/null")){
44 logger("Remote IP for host $remotehostname has changed or is not connected, restarting connection to $remoteip.");
45 system("/usr/local/bin/ipsecctrl S $settings[0]");
46 next;
47 }
ad60e3ea
CS
48 if ($debug){logger("All connections may be fine nothing was done.");}
49 }
dced81b2
CS
50}
51
52sub logger {
53 my $log = shift;
54 system("logger -t vpnwatch \"$log\"");
55}