Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
pfsense-choose_openvpn_restart
#!/usr/local/bin/php
#
# script based on https://dhampir.no/stuff/bash/pfsense-vpnreset
# verified to work for OpenVPN clients in pfSense 2.4.2
# use at your own risk
#
# script based on https://gist.github.com/patricknwn/4acd813f43ac7786ca6a6fe71419ef17
# verified to work for OpenVPN clients in pfSense 2.4.2
# use at your own risk
# 15 Oct 2021 - Harshad Kakatkar
# I have tested this script on 2.5.2,
# Please test thoroughly before use, Also no liabilities, Use at your own risk.
# usage: scriptname <vpnname>
# vpnname: The name of the VPN connection, as shown in the web interface "OpenVPN: Client" description field.
# You can put the vpnname in quotes if you need spaces.
<?php
# Preload
require_once('service-utils.inc');
require_once("ipsec.inc");
{
$inputid= $argv[1];
print "Restart $inputid vpn \n";
}
# Init
function fubar($reason) {print $reason . "\n"; exit(1);}
$xml=simplexml_load_file("/cf/conf/config.xml") or fubar("Error: Cannot load config.xml");
$found = false;
$sleeptime = 20;
# OpenVPN
foreach($xml->openvpn->{'openvpn-client'} as $vpn) {
$desc = html_entity_decode((string)$vpn->description);
print "Found OpenVPN: $desc\n";
# code figured out from /etc/inc/service-utils.inc
if ($inputid == $vpn->description)
{
print "Matching with input name $inputid, Restarting VPN $vpn->description \n";
service_control_restart("openvpn", array('vpnmode' => 'client', 'id' => $vpn->vpnid));
}
else
{
print "Skipping $vpn->description \n";
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment