#!/usr/bin/perl ################################################## ##### VPN-Watch.pl Version 0.1 ##### ################################################## use strict; require '/var/ipfire/general-functions.pl'; my @vpnsettings; my $i = 0; my $file = "/var/run/vpn-watch.pid"; system("echo $$ > $file"); while ( $i == 0){ if (open(FILE, "${General::swroot}/vpn/config")) { @vpnsettings = ; close(FILE); unless(@vpnsettings) {exit 1;} } foreach (@vpnsettings){ my @settings = split(/,/,$_); if ($settings[27] ne 'RED'){next;} if ($settings[4] ne 'net'){next;} if ($settings[1] ne 'on'){next;}chomp($settings[29]); if ($settings[29] ne 'on'){next;} my $remotehostname = $settings[11]; my $remoteip = `/usr/bin/ping -c 1 $remotehostname 2>/dev/null | head -n1 | awk '{print \$3}' | tr -d '()' | tr -d ':'`;chomp($remoteip); if ($remoteip eq ""){next;} if (system("ipsec whack --status | grep $remoteip >/dev/null")){ logger("Remote IP for host $remotehostname has changed or is not connected, restarting connection to $remoteip."); system("/usr/local/bin/ipsecctrl S $settings[0]"); next; } my $hostip = `cat /var/ipfire/red/local-ipaddress 2>/dev/null`; chomp($hostip); if (system("ipsec whack --status | grep $hostip >/dev/null")){ logger("Our own IP has changed to $hostip restarting connection."); system("/usr/local/bin/ipsecctrl S $settings[1]"); next;} } logger("Nothing was done."); sleep(300); } sub logger { my $log = shift; system("logger -t vpnwatch \"$log\""); }