Skip to content

Instantly share code, notes, and snippets.

@Aketzu
Created March 15, 2020 14:28
Show Gist options
  • Save Aketzu/ecf8dd00fcd5f9d9644cea680cf5eee6 to your computer and use it in GitHub Desktop.
Save Aketzu/ecf8dd00fcd5f9d9644cea680cf5eee6 to your computer and use it in GitHub Desktop.
#!/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