Skip to content

Instantly share code, notes, and snippets.

@perfecto25
Last active May 8, 2025 14:13
Show Gist options
  • Save perfecto25/6744d337aa1bce861a2c2aa28dc41a4a to your computer and use it in GitHub Desktop.
Save perfecto25/6744d337aa1bce861a2c2aa28dc41a4a to your computer and use it in GitHub Desktop.
OpenVPN check connected users
#!/bin/bash
## checks openvpn status of connected users, shows connected users in table format
## returns >
## username: <virtual ip>, [x days, x hours, x minutes] from <user's public IP>
##
## make sure your openvpn server config has this line uncommented:
## management 127.0.0.1 5555
current_epoch=$(date +%s)
function uptime {
given_epoch=$(date -d "${1}" +%s 2>/dev/null)
elapsed_seconds=$((current_epoch - given_epoch))
days=$((elapsed_seconds / 86400))
hours=$(( (elapsed_seconds % 86400) / 3600 ))
minutes=$(( (elapsed_seconds % 3600) / 60 ))
echo "${days} days, ${hours} hours, ${minutes} minutes"
}
output=$((echo "status"; sleep 1; echo "quit") | nc localhost 5555)
declare -A users
output1=$(echo "$output" | sed -n '/Bytes Sent/,/ROUTING TABLE/{/Bytes Sent/d; /ROUTING TABLE/d; p}')
## get public IP and uptime of each user
while IFS= read -r line; do
user=$(echo $line | awk -F',' {'print $1'})
remote_ip=$(echo $line | awk -F',' {'print $2'} | awk -F':' {'print $1'})
login=$(echo $line | awk -F',' {'print $5'})
login_date=$(uptime "${login}")
users[$user]="[${login_date}] from ${remote_ip}"
done <<< "$output1"
## get internal VPN IP of each user (virtual address)
output2=$(echo "$output" | sed -n '/Last Ref/,/GLOBAL STATS/{/Last Ref/d; /GLOBAL STATS/d; p}')
while IFS= read -r line;
do
user=$(echo $line | awk -F',' {'print $2'})
virtual_ip=$(echo $line | awk -F',' {'print $1'} | tr -d '\n')
users[$user]="${virtual_ip}, ${users[$user]}"
done <<< "$output2"
# sort final data
for user in $(for k in "${!users[@]}"; do echo "$k"; done | sort); do
echo "${user}: ${users[$user]}"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment