Je t'ai mis la source complète, car l'adresse ne fct plus
http://www.ixus.net/modules.php?mop=mod ... 8&forum=10
Bonjour,
Aprés avoir lu à plusieurs reprise le mal de certain à relancer leurs connections VPN (Dyndns): voici ce qui va vous changer la vie.
et vous évitera ce message:
"packet from IP:500: initial Main Mode message received on IP:500 but no connection has been authorized"
Voici, le script "ipsecmon.pl" perl à installer dans /usr/local/bin/ :
#------------------couper ici-------------------------------
#!/usr/bin/perl
# ipsec_monitor for FreeS/WAN
# Copyright (C) 2003 by Tim Niemueller <tim@niemueller.de>
# Website:
http://www.niemueller.de/software/perl/ipsecmonitor
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#### Modules
use strict;
use Getopt::Long;
use POSIX qw(setsid);
use Fcntl ':flock';
#### Constants, just to make code readable
my $VERSION="0.1";
my $ipsec="/usr/sbin/ipsec"; ########## Fonction de votre distrib
#### Get parameters
my %params=();
GetOptions("conn=s" => \\$params{'conn'},
"host=s" => \\$params{'host'},
"script:s" => \\$params{'script'},
"t:i" => \\$params{'t'},
"d" => \\$params{'d'},
"s" => \\$params{'s'},
) or usage();
if ( ($params{'conn'} eq "") || ($params{'host'} eq "") ) {
usage();
}
if ( ($params{'script'} ne "") && (! -x $params{'script'}) ) {
failure_exit("The script file does not exist or is not executable.");
}
if ($params{'t'} eq "") {
$params{'t'} = 180;
}
if ($params{'s'}) {
use Sys::Syslog qw(:DEFAULT setlogsock);
setlogsock('unix');
openlog("ipsec_monitor($params{'conn'})", "", "authpriv");
}
# Where should we create the lock file?
my $lockfile = "/var/lock/ipsec_monitor.$params{'conn'}.lock";
#### Signals
$SIG{'TERM'} = \\&terminate_daemon;
$SIG{'HUP'} = \\&check_status;
#### Globals to make it quick and dirty
my $oldaddr = "";
#### Main Program, main() like
daemonize();
create_lock();
while (1) {
my $oa = $oldaddr;
my $na = check_status();
# logmsg("debug", "IP checked: ".join(".", unpack('C4', $oa))." -> ".join(".", unpack('C4', $na)));
sleep $params{'t'};
}
#### Subs related to time handling and alarms
sub check_status {
my $newaddr=gethostbyname($params{'host'});
if ($newaddr ne $oldaddr) {
# Address has changed, replace connection
logmsg("info", "IP change detected: ".join(".", unpack('C4', $oldaddr))." -> ".join(".", unpack('C4', $newaddr)));
if ($oldaddr ne "") {
# On Startup this is empty of course...
logmsg("info", "Replacing connection");
system("$ipsec auto --replace $params{'conn'}");
if ($params{'script'}) {
# We have a script file, wait 60 seconds and then excute it
sleep 60;
system($params{'script'});
}
}
$oldaddr = $newaddr;
}
return $newaddr;
}
#### Subs related to basic program stuff (daemon, fifo, lock etc.)
# Could be modified to syslog for example
sub logmsg {
my $priority = shift;
my $msg = shift;
if ($params{'d'}) {
my $now=localtime();
print "$now ($priority): ".printf($msg, @_)."\\n";
} else {
if ($params{'s'}) {
syslog($priority, $msg, @_);
}
}
}
# logs the errors and exits the program
sub failure_exit {
logmsg($_[0]);
die $_[0];
}
# Disconnects from console
sub daemonize {
if (! $params{'d'}) {
my $pid;
chdir '/' or failure_exit("Can't chdir to /: $!");
open STDIN, '/dev/null' or failure_exit("daemonize: Can't read /dev/null: $!");
open STDOUT, '>/dev/null' or failure_exit("daemonize: Can't write to /dev/null: $!");
defined($pid = fork) or failure_exit("Can't fork: $!");
exit 0 if $pid;
setsid() or failure_exit("Can't start a new session: $!");
print LOCK $$;
logmsg("info", "Started successfully");
}
}
# creates the lockfile
sub create_lock {
open(LOCK, ">$lockfile");
my $ok = flock(LOCK, LOCK_EX | LOCK_NB);
failure_exit("It seems that another instance is already running") if (! $ok);
}
# removes the lockfile
sub remove_lock {
flock(LOCK, LOCK_UN);
close(LOCK);
unlink $lockfile;
}
# Terminates daemon closing ISDN connection,
# used as signal handler
sub terminate_daemon {
alarm 0; # Stop timer
remove_lock();
logmsg("info", "Stopped");
if ($params{'s'}) {
closelog();
}
exit;
}
# prints some basic usage message
sub usage {
print "ipsec_monitor v$VERSION\\n",
"Copyright (C) 2003 by Tim Niemueller\\n\\n",
"Monitors a hostname and when it changes replaces the IPSec\\n",
"connection (FreeS/WAN style) to get the link up again\\n\\n",
"Usage: ipsec_monnitor --conn=CONNECTIONNAME \\\\\\n",
" --host=HOSTNAME [options]\\n",
"where options are:\\n",
"--conn=CONN: FreeS/WAN Connection name\\n",
"--host=HOST: Hostname of the connection to be monitored\\n",
"--script=..: Path to an additional script that is executed 1 minute\\n",
" after the connection has been replaced. For example routing\\n",
" stuff that needs to be done. Script must be executable!\\n",
"-t t : check every t seconds if connection parameters have changed\\n",
" Dafault: 180 seconds\\n",
"-d : Debug mode. Do not fork to background, log output to STDOUT\\n",
"-s : Use Syslog for logging. Default is no logging.\\n",
"\\n";
exit 1;
}
#--------------------------------couper ici-------------------------------------------------
Donc voila une fois le script installé, il faut le rendre executable (chmod 755 ipsecmon.pl).
Voici un script "reload.sh" qui va relancer une conn vpn, à ajouter dans /usr/local/bin
#----------------------couper ici---------------------------------------
#!/bin/sh
echo "redemarrage ipsec vpnclient1";
/usr/sbin/ipsec auto --replace vpnclient1
/usr/sbin/ipsec auto --rereadsecrets
/usr/sbin/ipsec auto --up vpnclient1
exit 0
#------------------------couper ici--------------------------------------
rendre le script executable (chmod 755 reload.sh)
VPNCLIENT1 est le nom du --conn dans ipsec.conf.
Voici la syntaxe à utiliser sur le serveur VPN:
perl /usr/local/bin/ipsemon.pl --conn VPNCLIENT1 --host=toto.riri.tata --script=/usr/local/bin/reload.sh -s
--HOST: c'est l'adresse de type dyndns (ou static), de votre VPNCLIENT1
Voila il reste plus qu'a reproduire fonction du nombre de client (VPNCLIENT2, VPNCLIENT3 etc...), et recreer un reload.sh par client (reload1.sh, reload2.sh etc...)
Voila en esperant que cela réponde à un besoin.
Si l'automobile avait progressé de la même façon que l'informatique, une Rolls-Royce couterait aujourd'hui 100 dollars, ferait 300.000 kilomètres avec un seul litre d'essence et exploserait une fois par an en tuant tous ses passagers. (Cringely)