Script to count number of logged in users to OpenVPN

To get that information you need to query your openvpn-status.log file, which usually resides in /var/log/openvpn/openvpn-status.log.

A normal cat will show the following:

OpenVPN CLIENT LIST
Updated,Wed Mar 5 11:57:51 2019
Common Name,Real Address,Bytes Received,Bytes Sent,Connected Since
someuser,someip:55530,100686479,319036434,Tue Mar 5 03:22:04 2019
someuser,someip:58064,23120958,141190549,Tue Mar 5 12:30:05 2019
someuser,someip:7115,18011423,65882394,Tue Mar 5 14:26:00 2019
someuser,someip:4463,79682826,444263153,Tue Mar 5 08:01:52 2019
ROUTING TABLE
Virtual Address,Common Name,Real Address,Last Ref
xxx.xxx.xxx.xxx,someuser,someip:47080,Tue Mar 5 12:51:44 2019
GLOBAL STATS
Max bcast/mcast queue length,46
END

The command to get number of connected users at any given time is:

echo $((sed '/ROUTING TABLE/Q' /var/log/openvpn/openvpn-status.log | wc -l-3)) "users connected on" date +"%m-%d-%Y %H:%M"
4 users connected on 03-05-2019 13:23 #Just an example, not real value

This basically parses the text till reaching the line with “ROUTING TABLE”, then counts the lines and subtracts 3 from them due to the first unneeded 3 lines

That’s it! Enjoy!

Sources:

About SoCRaT

Systems Engineer, OSS & Linux Geek
This entry was posted in Linux and tagged , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s