mirror of
https://github.com/pivpn/pivpn.git
synced 2024-12-18 19:00:15 +00:00
Show connected clients data rates with dotted decimal notation
This commit is contained in:
parent
eae70d0295
commit
6653d4caa3
4 changed files with 152 additions and 57 deletions
|
@ -3,16 +3,28 @@
|
||||||
|
|
||||||
STATUS_LOG="/var/log/openvpn-status.log"
|
STATUS_LOG="/var/log/openvpn-status.log"
|
||||||
|
|
||||||
function hr() {
|
|
||||||
numfmt --to=iec-i --suffix=B "$1"
|
|
||||||
}
|
|
||||||
|
|
||||||
printf "\n"
|
|
||||||
if [ ! -f "${STATUS_LOG}" ]; then
|
if [ ! -f "${STATUS_LOG}" ]; then
|
||||||
echo "The file: $STATUS_LOG was not found!"
|
echo "The file: $STATUS_LOG was not found!"
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
scriptusage(){
|
||||||
|
echo "::: List any connected clients to the server"
|
||||||
|
echo ":::"
|
||||||
|
echo "::: Usage: pivpn <-c|clients> [-b|bytes]"
|
||||||
|
echo ":::"
|
||||||
|
echo "::: Commands:"
|
||||||
|
echo "::: [none] List clients with human readable format"
|
||||||
|
echo "::: -b, bytes List clients with dotted decimal notation"
|
||||||
|
echo "::: -h, help Show this usage dialog"
|
||||||
|
}
|
||||||
|
|
||||||
|
hr(){
|
||||||
|
numfmt --to=iec-i --suffix=B "$1"
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
listClients(){
|
||||||
printf ": NOTE : The output below is NOT real-time!\n"
|
printf ": NOTE : The output below is NOT real-time!\n"
|
||||||
printf ": : It may be off by a few minutes.\n"
|
printf ": : It may be off by a few minutes.\n"
|
||||||
printf "\n"
|
printf "\n"
|
||||||
|
@ -23,16 +35,50 @@ printf "\e[4mName\e[0m \t \e[4mRemote IP\e[0m \t \e[4mVirtual IP\e[0m \t \
|
||||||
|
|
||||||
if grep -q "^CLIENT_LIST" "${STATUS_LOG}"; then
|
if grep -q "^CLIENT_LIST" "${STATUS_LOG}"; then
|
||||||
if [ -n "$(type -t numfmt)" ]; then
|
if [ -n "$(type -t numfmt)" ]; then
|
||||||
|
if [ "$HR" = 1 ]; then
|
||||||
while read -r line; do
|
while read -r line; do
|
||||||
read -r -a array <<< $line
|
read -r -a array <<< $line
|
||||||
[[ ${array[0]} = CLIENT_LIST ]] || continue
|
[[ ${array[0]} = CLIENT_LIST ]] || continue
|
||||||
printf "%s \t %s \t %s \t %s \t %s \t %s %s %s - %s\n" ${array[1]} ${array[2]} ${array[3]} $(hr ${array[4]}) $(hr ${array[5]}) ${array[7]} ${array[8]} ${array[10]} ${array[9]}
|
printf "%s \t %s \t %s \t %s \t %s \t %s %s %s - %s\n" ${array[1]} ${array[2]} ${array[3]} $(hr ${array[4]}) $(hr ${array[5]}) ${array[7]} ${array[8]} ${array[10]} ${array[9]}
|
||||||
done <$STATUS_LOG
|
done <$STATUS_LOG
|
||||||
|
else
|
||||||
|
while read -r line; do
|
||||||
|
read -r -a array <<< $line
|
||||||
|
[[ ${array[0]} = CLIENT_LIST ]] || continue
|
||||||
|
printf "%s \t %s \t %s \t %'d \t %'d \t %s %s %s - %s\n" ${array[1]} ${array[2]} ${array[3]} ${array[4]} ${array[5]} ${array[7]} ${array[8]} ${array[10]} ${array[9]}
|
||||||
|
done <$STATUS_LOG
|
||||||
|
fi
|
||||||
else
|
else
|
||||||
awk -F' ' -v s='CLIENT_LIST' '$1 == s {print $2"\t\t"$3"\t"$4"\t"$5"\t\t"$6"\t\t"$8" "$9" "$11" - "$10"\n"}' ${STATUS_LOG}
|
awk -F' ' -v s='CLIENT_LIST' '$1 == s {print $2"\t\t"$3"\t"$4"\t"$5"\t\t"$6"\t\t"$8" "$9" "$11" - "$10"\n"}' ${STATUS_LOG}
|
||||||
fi
|
fi
|
||||||
else
|
else
|
||||||
printf "\nNo Clients Connected!\n"
|
printf "\nNo Clients Connected!\n"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
printf "\n"
|
printf "\n"
|
||||||
} | column -t -s $'\t'
|
} | column -t -s $'\t'
|
||||||
|
}
|
||||||
|
|
||||||
|
if [[ $# -eq 0 ]]; then
|
||||||
|
HR=1
|
||||||
|
listClients
|
||||||
|
else
|
||||||
|
while true; do
|
||||||
|
case "$1" in
|
||||||
|
-b|bytes)
|
||||||
|
HR=0
|
||||||
|
listClients
|
||||||
|
exit 0
|
||||||
|
;;
|
||||||
|
-h|help)
|
||||||
|
scriptusage
|
||||||
|
exit 0
|
||||||
|
;;
|
||||||
|
*)
|
||||||
|
HR=0
|
||||||
|
listClients
|
||||||
|
exit 0
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
done
|
||||||
|
fi
|
||||||
|
|
|
@ -17,7 +17,8 @@ function makeOVPNFunc {
|
||||||
}
|
}
|
||||||
|
|
||||||
function listClientsFunc {
|
function listClientsFunc {
|
||||||
$SUDO /opt/pivpn/clientStat.sh
|
shift
|
||||||
|
$SUDO /opt/pivpn/clientStat.sh "$@"
|
||||||
exit 0
|
exit 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -93,7 +94,7 @@ fi
|
||||||
# Handle redirecting to specific functions based on arguments
|
# Handle redirecting to specific functions based on arguments
|
||||||
case "$1" in
|
case "$1" in
|
||||||
"-a" | "add" ) makeOVPNFunc "$@";;
|
"-a" | "add" ) makeOVPNFunc "$@";;
|
||||||
"-c" | "clients" ) listClientsFunc;;
|
"-c" | "clients" ) listClientsFunc "$@";;
|
||||||
"-d" | "debug" ) debugFunc;;
|
"-d" | "debug" ) debugFunc;;
|
||||||
"-l" | "list" ) listOVPNFunc;;
|
"-l" | "list" ) listOVPNFunc;;
|
||||||
"-r" | "revoke" ) removeOVPNFunc "$@";;
|
"-r" | "revoke" ) removeOVPNFunc "$@";;
|
||||||
|
|
|
@ -1,15 +1,29 @@
|
||||||
#!/bin/bash
|
#!/usr/bin/env bash
|
||||||
|
# PiVPN: client status script
|
||||||
|
|
||||||
cd /etc/wireguard/configs
|
CLIENTS_FILE="/etc/wireguard/configs/clients.txt"
|
||||||
if [ ! -s clients.txt ]; then
|
|
||||||
|
if [ ! -s "$CLIENTS_FILE" ]; then
|
||||||
echo "::: There are no clients to list"
|
echo "::: There are no clients to list"
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
scriptusage(){
|
||||||
|
echo "::: List any connected clients to the server"
|
||||||
|
echo ":::"
|
||||||
|
echo "::: Usage: pivpn <-c|clients> [-b|bytes]"
|
||||||
|
echo ":::"
|
||||||
|
echo "::: Commands:"
|
||||||
|
echo "::: [none] List clients with human readable format"
|
||||||
|
echo "::: -b, bytes List clients with dotted decimal notation"
|
||||||
|
echo "::: -h, help Show this usage dialog"
|
||||||
|
}
|
||||||
|
|
||||||
hr(){
|
hr(){
|
||||||
numfmt --to=iec-i --suffix=B "$1"
|
numfmt --to=iec-i --suffix=B "$1"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
listClients(){
|
||||||
if DUMP="$(wg show wg0 dump)"; then
|
if DUMP="$(wg show wg0 dump)"; then
|
||||||
DUMP="$(tail -n +2 <<< "$DUMP")"
|
DUMP="$(tail -n +2 <<< "$DUMP")"
|
||||||
else
|
else
|
||||||
|
@ -29,15 +43,48 @@ while IFS= read -r LINE; do
|
||||||
BYTES_RECEIVED="$(awk '{ print $6 }' <<< "$LINE")"
|
BYTES_RECEIVED="$(awk '{ print $6 }' <<< "$LINE")"
|
||||||
BYTES_SENT="$(awk '{ print $7 }' <<< "$LINE")"
|
BYTES_SENT="$(awk '{ print $7 }' <<< "$LINE")"
|
||||||
LAST_SEEN="$(awk '{ print $5 }' <<< "$LINE")"
|
LAST_SEEN="$(awk '{ print $5 }' <<< "$LINE")"
|
||||||
CLIENT_NAME="$(grep "$PUBLIC_KEY" clients.txt | awk '{ print $1 }')"
|
CLIENT_NAME="$(grep "$PUBLIC_KEY" "$CLIENTS_FILE" | awk '{ print $1 }')"
|
||||||
|
|
||||||
|
if [ "$HR" = 1 ]; then
|
||||||
if [ "$LAST_SEEN" -ne 0 ]; then
|
if [ "$LAST_SEEN" -ne 0 ]; then
|
||||||
printf "%s \t %s \t %s \t %s \t %s \t %s\n" "$CLIENT_NAME" "$REMOTE_IP" "${VIRTUAL_IP/\/32/}" "$(hr "$BYTES_RECEIVED")" "$(hr "$BYTES_SENT")" "$(date -d @"$LAST_SEEN" '+%b %d %Y - %T')"
|
printf "%s \t %s \t %s \t %s \t %s \t %s\n" "$CLIENT_NAME" "$REMOTE_IP" "${VIRTUAL_IP/\/32/}" "$(hr "$BYTES_RECEIVED")" "$(hr "$BYTES_SENT")" "$(date -d @"$LAST_SEEN" '+%b %d %Y - %T')"
|
||||||
else
|
else
|
||||||
printf "%s \t %s \t %s \t %s \t %s \t %s\n" "$CLIENT_NAME" "$REMOTE_IP" "${VIRTUAL_IP/\/32/}" "$(hr "$BYTES_RECEIVED")" "$(hr "$BYTES_SENT")" "(not yet)"
|
printf "%s \t %s \t %s \t %s \t %s \t %s\n" "$CLIENT_NAME" "$REMOTE_IP" "${VIRTUAL_IP/\/32/}" "$(hr "$BYTES_RECEIVED")" "$(hr "$BYTES_SENT")" "(not yet)"
|
||||||
fi
|
fi
|
||||||
|
else
|
||||||
|
if [ "$LAST_SEEN" -ne 0 ]; then
|
||||||
|
printf "%s \t %s \t %s \t %'d \t %'d \t %s\n" "$CLIENT_NAME" "$REMOTE_IP" "${VIRTUAL_IP/\/32/}" "$BYTES_RECEIVED" "$BYTES_SENT" "$(date -d @"$LAST_SEEN" '+%b %d %Y - %T')"
|
||||||
|
else
|
||||||
|
printf "%s \t %s \t %s \t %'d \t %'d \t %s\n" "$CLIENT_NAME" "$REMOTE_IP" "${VIRTUAL_IP/\/32/}" "$BYTES_RECEIVED" "$BYTES_SENT" "(not yet)"
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
done <<< "$DUMP"
|
done <<< "$DUMP"
|
||||||
|
|
||||||
printf "\n"
|
printf "\n"
|
||||||
} | column -t -s $'\t'
|
} | column -t -s $'\t'
|
||||||
|
}
|
||||||
|
|
||||||
|
if [[ $# -eq 0 ]]; then
|
||||||
|
HR=1
|
||||||
|
listClients
|
||||||
|
else
|
||||||
|
while true; do
|
||||||
|
case "$1" in
|
||||||
|
-b|bytes)
|
||||||
|
HR=0
|
||||||
|
listClients
|
||||||
|
exit 0
|
||||||
|
;;
|
||||||
|
-h|help)
|
||||||
|
scriptusage
|
||||||
|
exit 0
|
||||||
|
;;
|
||||||
|
*)
|
||||||
|
HR=0
|
||||||
|
listClients
|
||||||
|
exit 0
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
done
|
||||||
|
fi
|
||||||
|
|
|
@ -17,7 +17,8 @@ makeConf(){
|
||||||
}
|
}
|
||||||
|
|
||||||
listConnected(){
|
listConnected(){
|
||||||
$SUDO /opt/pivpn/clientSTAT.sh
|
shift
|
||||||
|
$SUDO /opt/pivpn/clientSTAT.sh "$@"
|
||||||
exit 0
|
exit 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -84,7 +85,7 @@ fi
|
||||||
# Handle redirecting to specific functions based on arguments
|
# Handle redirecting to specific functions based on arguments
|
||||||
case "$1" in
|
case "$1" in
|
||||||
"-a" | "add" ) makeConf "$@";;
|
"-a" | "add" ) makeConf "$@";;
|
||||||
"-c" | "clients" ) listConnected;;
|
"-c" | "clients" ) listConnected "$@";;
|
||||||
"-d" | "debug" ) debug;;
|
"-d" | "debug" ) debug;;
|
||||||
"-l" | "list" ) listClients;;
|
"-l" | "list" ) listClients;;
|
||||||
"-qr" | "qrcode" ) showQrcode "$@";;
|
"-qr" | "qrcode" ) showQrcode "$@";;
|
||||||
|
|
Loading…
Reference in a new issue