Skip to main content

OpenVPN Management Interface Python API

PyPI

Summary

[!IMPORTANT] This project is a fork of the Jamie-/openvpn-api project. It presents support for receiving events and registering callbacks for them, which is not present in the original version. The original repository is abandoned; this is an attempt at reviving it.

A Python API for interacting with the OpenVPN management interface.

Very useful for extracting metrics and status from OpenVPN server management interfaces, and authorizing users with an external source.

This project was inspired by the work of Marcus Furlong in creating openvpn-monitor. It also uses openvpn-status by Jiangge Zhang for parsing the output of the OpenVPN status command as there's no point reinventing the wheel when an excellent solution already exists.

Release notes can be found here on GitHub.

:warning: This project is not yet stable and there are no guarantees for it to work. You are welcome to submit any bugs you may encounter, and I'll be happy to help.

When using and developing this library, you may find the manual for the OpenVPN management interface useful: https://openvpn.net/community-resources/management-interface/

Requirements

This project requires Python >= 3.6.

Dependencies:

Installation

Via PyPI

pip install openvpn-management-api

Via Source

git clone https://github.com/HosseyNJF/openvpn-management-api.git
cd openvpn-management-api
python setup.py install

Usage

Introduction

Create a VPN object for your management interface connection.

import openvpn_management_api.VPN

v = openvpn_management_api.VPN('localhost', 7505)

Then you can either manage connection and disconnection yourself

v.connect()
# Do some stuff, e.g.
print(v.release)
v.disconnect()

If the connection is successful, v.connect() will return True. However, if the connection fails v.connect() will raise an openvpn_management_api.errors.ConnectError exception with the reason for the connection failure.

Or use the connection context manager

with v.connection():
    # Do some stuff, e.g.
    print(v.release)

After initialising a VPN object, we can query specifics about it.

We can get the address we're communicating to the management interface on

>>> v.mgmt_address
'localhost:7505'

And also see if this is via TCP/IP or a Unix socket

>>> v.type
'ip'

or

>>> v.type
'socket'

These are represented by the VPNType class as VPNType.IP or VPNType.UNIX_SOCKET

>>> v.type
'ip'
>>> v.type == openvpn_management_api.VPNType.IP
True

Consume events

The management interface emits events on specific occasions, as specified in this documentation, that can be consumed using this library:

def event_handler(event: ClientEvent):
    print(f"Received event: " + event.type)

v.register_callback(ClientEvent, event_handler)

Available events

Two classes of events may be emitted by the server:

UpDownEvent

This event is fired when the server shuts down or is started.

Available properties:

event.type - either "UP" or "DOWN"

ClientEvent

This event is fired on client activity, such as CONNECT, REAUTH, ESTABLISHED, DISCONNECT, and ADDRESS.

Available properties:

Name Value Type
event_type Enum[CONNECT, REAUTH, ESTABLISHED, DISCONNECT, ADDRESS]
client_id int
key_id Optional[int]
primary Optional[int]
address Optional[str]
environment Dict[str, str]

Known limitations

  • Callbacks should be kept as lightweight as possible and not perform any heavy or time-consuming computation. If you need more, add the event to a queue and consume it separately.
  • Never send an OpenVPN command inside a callback. instead, add it to your own queue and process it outside the callback .

Daemon Interaction

All the properties that get information about the OpenVPN service you're connected to are stateful. The first time you call one of these methods it caches the information it needs so future calls are super fast. The information cached is unlikely to change often, unlike the status and metrics we can also fetch which are likely to change very frequently.

We can fetch the release string for the version of OpenVPN we're using

>>> v.release
'OpenVPN 2.4.4 x86_64-pc-linux-gnu [SSL (OpenSSL)] [LZO] [LZ4] [EPOLL] [PKCS11] [MH/PKTINFO] [AEAD] built on Sep  5 2018'

Or just the version number

>>> v.version
'2.4.4'

The information cached by these 2 properties can be cleared and will be repopulated on the next call

v.clear_cache()

Daemon State

We can get more information about the service by looking at it's state which is returned as a State object

>>> v.get_state()
<State desc='SUCCESS', mode='server'>

The State object contains the following things:

The daemon's current mode, client or server

>>> s = v.get_state()
>>> s.mode
'server'

Date and time the daemon was started

>>> s.up_since
datetime.datetime(2019, 6, 5, 23, 3, 21)

The daemon's current state

>>> s.state_name
'CONNECTED'

Which can be any of:

  • CONNECTING - OpenVPN's initial state.
  • WAIT - (Client only) Waiting for initial response from server.
  • AUTH - (Client only) Authenticating with server.
  • GET_CONFIG - (Client only) Downloading configuration options from server.
  • ASSIGN_IP - Assigning IP address to virtual network interface.
  • ADD_ROUTES - Adding routes to system.
  • CONNECTED - Initialization Sequence Completed.
  • RECONNECTING - A restart has occurred.
  • EXITING - A graceful exit is in progress.
  • RESOLVE - (Client only) DNS lookup
  • TCP_CONNECT - (Client only) Connecting to TCP server

The descriptive string - unclear from the OpenVPN documentation quite what this is, usually SUCCESS or the reason for disconnection if the state is RECONNECTING or EXITING

>>> s.desc_string
'SUCCESS'

The daemon's local virtual (VPN internal) v4 address, returned as an ipaddress.IPv4Address for ease of sorting, it can be easily converted to a string with str()

>>> s.local_virtual_v4_addr
IPv4Address('10.0.0.1')
>>> str(s.local_virtual_v4_addr)
'10.0.0.1'

If the daemon is in client mode, then remote_addr and remote_port will be populated with the address and port of the remote server

>>> s.remote_addr
IPv4Address('1.2.3.4')
>>> s.remote_port
1194

If the daemon is in server mode, then local_addr and local_port will be populated with the address and port of the exposed server

>>> s.local_addr
IPv4Address('5.6.7.8')
>>> s.local_port
1194

If the daemon is using IPv6 instead of, or in addition to, IPv4 then the there is also a field for the local virtual (VPN internal) v6 address

>>> s.local_virtual_v6_addr
'2001:db8:85a3::8a2e:370:7334'

Daemon Status

The daemon status is parsed from the management interface by openvpn_status an existing Python library for parsing the output from OpenVPN's status response. The code for which can be found in it's GitHub repo: https://github.com/tonyseek/openvpn-status

Therefore when we fetch the status from the OpenVPN daemon, it'll be returned using their models. For more information see their docs: https://openvpn-status.readthedocs.io/en/latest/api.html

Unlike the VPN state, the status is not stateful as it's output is highly likely to change between calls. Every time the status is requested, the management interface is queried for the latest data.

A brief example:

>>> status = v.get_status()
>>> status
<openvpn_status.models.Status object at 0x7f5eb54a2d68>
>>> status.client_list
OrderedDict([('1.2.3.4:56789', <openvpn_status.models.Client object at 0x7f5eb54a2128>)])

Metadata

Release files for openvpn-management-api 0.4.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for openvpn-management-api 0.4.0
File Size Uploaded Provenance Actions
openvpn_management_api-0.4.0.tar.gz 21.9 kB With provenance Details for openvpn_management_api-0.4.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for openvpn-management-api 0.4.0
File Interpreter ABI Platform Actions
openvpn_management_api-0.4.0-py3-none-any.whl Python 3 none any Details for openvpn_management_api-0.4.0-py3-none-any.whl

Total release size: 40.1 kB

Release files / openvpn_management_api-0.4.0.tar.gz

Download URL openvpn_management_api-0.4.0.tar.gz
Size 21.9 kB
Tags Source
SHA-256 checksum
How to use checksums
e14329f95c3ef8fd5077b6eccccd2176048b3e3c17629ffbd24119aad5d9e4bf
BLAKE2b-256 checksum
How to use checksums
2f72d21fdf2f774360f4d69b9017437e0b1afccbe272fe75d10076760fd0575d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.12.9

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on May 20, 2025.

Transparency log

Release files / openvpn_management_api-0.4.0-py3-none-any.whl

Download URL openvpn_management_api-0.4.0-py3-none-any.whl
Size 18.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ebedc84c19edda0e7646445a4301f14826afe1bf143199ea28f7d25cff841a0c
BLAKE2b-256 checksum
How to use checksums
de28b61b15c46d4bd315c945bb159db6ad5b2bd5cd5697575395dc9199b3247a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.12.9

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on May 20, 2025.

Transparency log

Security for openvpn-management-api 0.4.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

All 2 files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for openvpn_management_api-0.4.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on May 20, 2025.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.4.0 This release

2 release files Full provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page