Created
March 15, 2020 14:28
-
-
Save Aketzu/ecf8dd00fcd5f9d9644cea680cf5eee6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/ruby | |
require 'vici' | |
require 'socket' | |
require 'syslog' | |
Syslog.open('ipsec-reconnect', Syslog::LOG_PID, Syslog::LOG_DAEMON | Syslog::LOG_LOCAL4) | |
# Requires uncommenting socket in /etc/strongswan.d/charon/vici.conf | |
v = Vici::Connection.new(UNIXSocket.new('/var/run/charon.vici')) | |
# Configured connections | |
conns = [] | |
v.list_conns do |conn| | |
conn.each do |_, cc| | |
conns += cc['children'].keys | |
end | |
end | |
# Currently connected | |
sas = [] | |
v.list_sas do |sa| | |
sa.each do |_, ss| | |
ss['child-sas'].each do |k, p| | |
sas << k if p['state'] == 'INSTALLED' | |
end | |
end | |
end | |
# Kick the rest | |
(conns - sas).each do |sa| | |
Syslog.log(Syslog::LOG_NOTICE, 'Initiating connection ' + sa) | |
v.initiate 'child' => sa, 'timeout' => -1 do |log| | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment