LNS User Search

A simple BASH scipt that searches all Cisco IOS LNS routers for a string using SNMPWalk to find where a single or group of subscribers are currently connected. There is also a php wrapper page so the script can be executed via a browser:

Download.

#!/bin/bash
# This bash script uses snmpwalk to scan LNS routers for a particualr string,
# to locate where they are logged on. It checks all LNS routers, it doesn't
# stop on first match 


# List of LNS routers, add/remove as required
#
LNS_ADDR[0]="10.0.0.1"
LNS_NAME[0]="lns1"
LNS_ADDR[1]="10.0.0.2"
LNS_NAME[1]="lns2"
LNS_ADDR[2]="10.0.0.3"
LNS_NAME[2]="lns3"


function lns_crawl {

    #INDEX=$1

    if [ -e ${LNS_NAME[$INDEX]}.txt ]; then rm -f ${LNS_NAME[$INDEX]}.txt; fi

    snmpwalk -v 3 -n "" -u snmpuser -a MD5 -A "SuperSafe" -x AES -X "SecretKey" -l authPriv ${LNS_ADDR[$INDEX]} 1.3.6.1.4.1.9.10.24.1.3.2.1.2 \
    | awk '{print $NF}' | grep -E $USER_STRING | sed -s "s/\"//g" >> ${LNS_NAME[$INDEX]}.txt

    if [ `wc -l ${LNS_NAME[$INDEX]}.txt | awk '{print $1}'` -ge 1 ]
    then

        while IFS='' read -r USERNAME;
        do

            echo "${LNS_NAME[$INDEX]}: $USERNAME" >> $OUTPUT

        done < ${LNS_NAME[$INDEX]}.txt

    fi

    rm -f ${LNS_NAME[$INDEX]}.txt

}


# Default settings
#
if [ -z "$1" ]
then
    echo "No search string given"
    exit 1
else
    USER_STRING="$1"
fi

cd `dirname $0`
if [ $? -ne 0 ]
then
    echo "Couldn't change to directory `dirname $0`"
    exit 1
fi

OUTPUT="./lns-checker-`date +'%Y-%m-%d--%H-%M-%S'`"
if [ -e "$OUTPUT" ]
then
    rm -f $OUTPUT
fi

touch $OUTPUT
if [ $? -ne 0 ]
then
    echo "Couldn't make temp file `pwd`/$OUTPUT"
    exit 1
fi


# Start the SNMP walk
#
echo "Searching for $USER_STRING"

INDEX=0
while [ $INDEX -lt ${#LNS_ADDR[@]} ]
do

    lns_crawl &

    let INDEX=INDEX+1

done

wait

echo "Results: `wc -l $OUTPUT | awk '{print $1}'`"
cat $OUTPUT
rm -f $OUTPUT

Previous page: Light (Optics) Calculators
Next page: Max MTU Size