Skip to content

Instantly share code, notes, and snippets.

@zerwes
Forked from rlsit/ipsec.pl
Created January 4, 2018 12:48
Show Gist options
  • Save zerwes/79dae179c920165c5410d4a6504ff16e to your computer and use it in GitHub Desktop.
Save zerwes/79dae179c920165c5410d4a6504ff16e to your computer and use it in GitHub Desktop.
sophos SG restart stalled ipsec tunnels
#! /usr/bin/perl -w
use strict;
use warnings;
use Astaro::ConfdPlRPC;
use Data::Dumper;
use Try::Tiny;
my $restartall = 0;
if ($#ARGV >= 0) {
if ($ARGV[0] eq "--all") {
$restartall = 1;
} else {
die "unhandled arg $ARGV[0]";
}
}
my $confd = new Astaro::ConfdPlRPC();
$confd->lock or die 'cannot lock';
my $ipsec = $confd->get_ipsec_status();
for my $tun (keys %$ipsec) {
#print "$tun ...\n";
if ($restartall or $ipsec->{$tun}->{all_established} != 1) {
if ($restartall or -f "/tmp/$tun") {
try {
my $con = $confd->get_object($tun);
$con->{data}->{status} = 0;
$confd->set_object($con);
$confd->commit;
sleep 2;
$con = $confd->get_object($tun);
$con->{data}->{status} = 1;
$confd->set_object($con);
$confd->commit;
sleep 1;
$con = $confd->get_object($tun);
print "$tun aka. $con->{data}->{name} restarted: status: $con->{data}->{status}\n";
if ( -f "/tmp/$tun") {
unlink("/tmp/$tun");
}
} catch {
print "ERROR occured while restarting $tun\n";
print Dumper($confd->err_get_extended()), "\n";
}
} else {
if (!$restartall) {
open(TFH, ">/tmp/$tun");
close(TFH);
}
}
} else {
if ( -f "/tmp/$tun") {
unlink("/tmp/$tun");
}
}
}
$confd->disconnect;
exit 0;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment