ojarva / openvpn-status-parser Goto Github PK
View Code? Open in Web Editor NEWParser for openvpn status file
License: MIT License
Parser for openvpn status file
License: MIT License
openvpn-status-parser uses the "Common Name" as the dict key in the connected_clients
attributes.
This leads to a problem when no client certificate is used to authenticate users, but only login/pass, as the "Common Name" field is "UNDEF" for all connected users.
A consequence of this is that when iterating through all connected clients, the same key is used, and its value is overwritten each time, resulting in a list containing only one client.
Another consequence (not yet tested but supposed), is that even if client certificates are used, if the same user has multiple active sessions on the same VPN server (because he has several terminals, or connection issue), only one active connection will be seen.
A fix for this would be to use "Client ID" (which is supposed to be unique) instead of "Common Name" as the key to the connected_clients
attribute.
I'll send a PR implementing this fix.
It seems my version of OpenVPN (2.5.1) has more fields in the client list than what is expected, which causes an exception while getting the client list from the parser object:
>>> from openvpn_status_parser import OpenVPNStatusParser
>>> import pprint
>>> parser = OpenVPNStatusParser("/tmp/status.log")
>>> pprint.pprint(parser.connected_clients)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/nicolas/git/openvpn-status-parser/openvpn_status_parser/__init__.py", line 141, in connected_clients
self._parse_file()
File "/home/nicolas/git/openvpn-status-parser/openvpn_status_parser/__init__.py", line 120, in _parse_file
self.title_processors[row_title](row)
File "/home/nicolas/git/openvpn-status-parser/openvpn_status_parser/__init__.py", line 83, in _process_client_list
self._connected_clients[row[1]]["connected_since"] = (datetime.datetime.fromtimestamp(int(row[-1])))
ValueError: invalid literal for int() with base 10: 'AES-256-CBC'
My OpenVPN server is configured with option "status-version 3".
Here is the (anonymized) status file :
TITLE OpenVPN 2.5.1 x86_64-pc-linux-gnu [SSL (OpenSSL)] [LZO] [LZ4] [EPOLL] [PKCS11] [MH/PKTINFO] [AEAD] built on May 14 2021
TIME 2021-12-16 16:13:52 1639667632
HEADER CLIENT_LIST Common Name Real Address Virtual Address Virtual IPv6 Address Bytes Received Bytes Sent Connected Since Connected Since (time_t) Username Client ID Peer ID Data Channel Cipher
CLIENT_LIST UNDEF 1.2.3.4:56736 10.42.42.2 67638680 45432322 2021-12-14 10:34:46 1639474486 my_user 609 0 AES-256-CBC
HEADER ROUTING_TABLE Virtual Address Common Name Real Address Last Ref Last Ref (time_t)
ROUTING_TABLE 96:03:2d:9d:0d:40@0 UNDEF 1.2.3.4:56736 2021-12-16 16:13:51 1639667631
GLOBAL_STATS Max bcast/mcast queue length 1
END
The issue is caused because openvpn-status-parser is expecting the "Connected Since (time_t)" to be the last field ( row[-1]
)
I'll submit a PR fixing this.
Hello, when I try to run following script on my VPS, I got "ImportError: cannot import name OpenVPNStatusParser"
Any idea where the problem could be?
Thanks in advance.
Here's my code:
`import re
from subprocess import *
from openvpn_status_parser import OpenVPNStatusParser
PORTS = [5555, 9876, 38388]
REGEX_PATTERN = re.compile(r'\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}')
parser = OpenVPNStatusParser("/etc/openvpn/openvpn-status.log")
vpn_list = []
for row in parser.connected_clients:
real_address = parser.connected_clients[row]['Real Address']
for match in re.findall(REGEX_PATTERN, real_address):
vpn_list.append(match)
adb_devices_output = check_output(["adb", "devices"])
adb_list = []
for match in re.findall(REGEX_PATTERN, adb_devices_output):
adb_list.append(match)
print( 'VPN "%s"' % vpn_list)
print( 'ADB "%s"' % adb_list)
diff_list = list(set(vpn_list) - set(adb_list))
print( 'DIFF "%s"' % diff_list)
for ip in diff_list:
for port in PORTS:
ip_with_port = ip + ':' + str(port)
result = check_output(["adb", "connect", ip_with_port])
if 'connected' in result:
print( 'Connected %s' % ip_with_port)
break
else:
print( 'Failed to connect %s' % ip_with_port)`
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.