kleine Anpassung an ipsec
[people/pmueller/ipfire-2.x.git] / src / scripts / vpn-watch
1 #!/usr/bin/perl
2 ##################################################
3 ##### VPN-Watch.pl Version 0.3 #####
4 ##################################################
5
6 use strict;
7
8 require '/var/ipfire/general-functions.pl';
9 my @vpnsettings;
10 my $i = 0;
11 my $file = "/var/run/vpn-watch.pid";
12 my $debug = 0;
13
14 if ( -e $file ){logger("There my be another vpn-watch runnning $file exists, vpn-watch will not start.");exit 1;}
15 if ($debug){logger("Call of vpn-watch pid not is not existing.");}
16
17 system("echo $$ > $file");
18
19 while ( $i == 0){
20 sleep(300);
21 if ($debug){logger("We will wait 300 seconds before next action.");}
22
23 if (open(FILE, "${General::swroot}/vpn/config")) {
24 @vpnsettings = <FILE>;
25 close(FILE);
26 unless(@vpnsettings) {exit 1;}
27 }
28
29 foreach (@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];
38
39 if ($debug){logger("Checking connection to $remotehostname.");}
40
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 }
48 if ($debug){logger("All connections may be fine nothing was done.");}
49 }
50 }
51
52 sub logger {
53 my $log = shift;
54 system("logger -t vpnwatch \"$log\"");
55 }