#!/bin/bash # This script shows status info of vpn/pptpd users getting from /var/log/messages (default). # Author: Andrey Makarov (pauk) (c) 2009 # Web: http://coreit.webhop.info # Version: 0.1.2 # # Usage: vlstat [/path/to-/logs/messages] [alive] # # "alive" - show only currently connected sessions # logfile="/var/log/messages" #------------------------------------------------------------ P_ALIVE="" for linecmd in "$*"; do if [ ! -z "$linecmd" ]; then if [ "$linecmd" == "alive" ]; then P_ALIVE="1" continue fi if [ ! -f "$linecmd" ]; then echo "File $linecmd not found" exit else logfile=$linecmd fi fi done bufIFS=$IFS IFS=" " strings=$(cat $logfile | awk '/pptpd|pppd/ { print }') bufpids=$(echo $strings | awk '{ print(substr($5,index($5,"[")+1,length($5)-index($5,"[")-2)) }' | sort -n | uniq ) cc="$(echo $bufpids | awk 'END {print NR}')" for (( i=1; i <= cc; i++)); do pids[${#pids[*]}]="$(echo $bufpids | awk -v iNR="$i" 'NR==iNR {print}')" done unset bufpids unset cc unset i for pid in "${pids[@]}"; do ptype=$(echo $strings | awk -v pid="$pid" '$5~pid { print(substr($5,0,index($5,"[")-1)); exit; }') if [ "$ptype" == "pptpd" ]; then srcip=$(echo $strings | awk -v pid="$pid" '$5~pid { if( index($0,"control connection started")>0 ) { print($8); exit; } }') continue else echo $strings | awk -v pid="$pid" -v alive="$P_ALIVE" -v srcip="$srcip" 'BEGIN { start_time=""; remote_ipaddr=""; end_time=""; connect_time=""; sent_recv=""; go="1"; if( srcip != "" ) status="connected"; else status=""; } $5~pid { if( index($0,"Connect:")>0 ) { start_time=$1" "$2" "$3; next; } if( index($0,"remote IP address")>0) { remote_ipaddr=$9; next; } if( index($0,"Connect time")>0) { connect_time=$8" "$9; next; } if( index($0,"Sent")>0) { sent_recv=$7"/"$10; next; } if( index($0,"Connection terminated")>0) { status="terminated"; end_time=$1" "$2" "$3; go="1"; next; } } END { if( status == "terminated") { if( alive != "" ) go="23"; } else { status="CONNECTED"; } if( start_time == "" ) start_time="h/z"; if( end_time == "" ) end_time="now!"; if( sent_recv == "" ) sent_recv="in process.."; if( connect_time == "" ) connect_time="h/z"; if( go == "1") { output="SRC: "srcip" pid: "pid" IP:"remote_ipaddr" STATUS:"status" connect-time:"connect_time; output2=" ("start_time"-"end_time") snt/recv:"sent_recv; print(output); print(output2); } }' fi done IFS=$bufIFS