]>
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 | ||
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.");} | |
c6c9630e | 16 | |
dced81b2 | 17 | system("echo $$ > $file"); |
c6c9630e | 18 | |
dced81b2 | 19 | while ( $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 | ||
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]; | |
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 | ||
52 | sub logger { | |
53 | my $log = shift; | |
54 | system("logger -t vpnwatch \"$log\""); | |
55 | } |