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