Answer the question
In order to leave comments, you need to log in
How can you determine when a client connects and disconnects from an OpenVPN server?
Is there any way to determine when a client connects and disconnects from the OpenVPN server, perhaps via an API? If so, how? If there is no standard way, then what are non-standard ones?
Answer the question
In order to leave comments, you need to log in
There are the following options for the server:
--client-connect cmd
--client-disconnect cmd
Add the following to the server config:
script-security 2 # to run scripts
client-disconnect "scripts/on-client-disconnect.sh"
Script preparation.
/etc/openvpn/script/on-client-disconnect.sh
#!/bin/bash
## Parse variables
if [ ! -z "${time_ascii}" ]; then
sessionStart=${time_ascii}
else
sessionStart="UNSET"
fi
if [ ! -z "${trusted_ip}" ]; then
clientHostAddress=${trusted_ip}
else
clientHostAddress="UNSET"
fi
if [ ! -z "${username}" ]; then
clientUID=${username}
else
clientUID="UNSET"
fi
if [ ! -z "${time_duration}" ]; then
sessionDuration=${time_duration}
else
sessionDuration="UNSET"
fi
if [ ! -z "${bytes_sent}" ]; then
txVolume=${bytes_sent}
else
txVolume="UNSET"
fi
if [ ! -z "${bytes_received}" ]; then
rxVolume=${bytes_received}
else
rxVolume="UNSET"
fi
if [ ! -z "${common_name}" ]; then
client_cn=${common_name}
else
client_cn="UNSET"
fi
## Send to syslog
logger -t openvpn -- "Disconnect: CommonName: ${client_cn} Username: ${clientUID} HostIP: ${clientHostAddress} Duration: ${sessionDuration} seconds opened at ${sessionStart} Session Traffic: TX: ${txVolume} bytes RX: ${rxVolume} bytes"
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question