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