]>
Commit | Line | Data |
---|---|---|
dced81b2 | 1 | #!/usr/bin/perl |
c6c9630e | 2 | ################################################## |
dced81b2 | 3 | ##### VPN-Watch.pl Version 0.1 ##### |
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"; | |
c6c9630e | 12 | |
dced81b2 | 13 | system("echo $$ > $file"); |
c6c9630e | 14 | |
dced81b2 CS |
15 | while ( $i == 0){ |
16 | if (open(FILE, "${General::swroot}/vpn/config")) { | |
17 | @vpnsettings = <FILE>; | |
18 | close(FILE); | |
19 | unless(@vpnsettings) {exit 1;} | |
20 | } | |
21 | ||
22 | foreach (@vpnsettings){ | |
23 | my @settings = split(/,/,$_); | |
24 | ||
25 | if ($settings[27] ne 'RED'){next;} | |
26 | if ($settings[4] ne 'net'){next;} | |
27 | if ($settings[1] ne 'on'){next;}chomp($settings[29]); | |
28 | if ($settings[29] ne 'on'){next;} | |
29 | ||
30 | my $remotehostname = $settings[11]; | |
31 | my $remoteip = `/usr/bin/ping -c 1 $remotehostname 2>/dev/null | head -n1 | awk '{print \$3}' | tr -d '()' | tr -d ':'`;chomp($remoteip); | |
32 | if ($remoteip eq ""){next;} | |
33 | if (system("ipsec whack --status | grep $remoteip >/dev/null")){ | |
34 | logger("Remote IP for host $remotehostname has changed or is not connected, restarting connection to $remoteip."); | |
35 | system("/usr/local/bin/ipsecctrl S $settings[0]"); | |
36 | next; | |
37 | } | |
38 | my $hostip = `cat /var/ipfire/red/local-ipaddress 2>/dev/null`; | |
39 | chomp($hostip); | |
40 | if (system("ipsec whack --status | grep $hostip >/dev/null")){ | |
41 | logger("Our own IP has changed to $hostip restarting connection."); | |
42 | system("/usr/local/bin/ipsecctrl S $settings[1]"); | |
43 | next;} | |
44 | } | |
45 | logger("Nothing was done."); | |
46 | sleep(300); | |
47 | } | |
48 | ||
49 | sub logger { | |
50 | my $log = shift; | |
51 | system("logger -t vpnwatch \"$log\""); | |
52 | } |