Skip to content

Instantly share code, notes, and snippets.

@pletch
Forked from anonymous/wl_snmpd.sh
Last active October 6, 2020 21:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save pletch/f0a9777db5db8639937233f552dc4ec1 to your computer and use it in GitHub Desktop.
Save pletch/f0a9777db5db8639937233f552dc4ec1 to your computer and use it in GitHub Desktop.
simple script to run a triggered snmp command get to wifi client info
#!/bin/sh
# Modified script to provide detailed wifi client info on atheros 9k based device with LEDE/OpenWRT. Script requires 'bc'
# package to output results. Output was designed to match the OID results from an Engenius EAP600 when queried via SNMP.
#
# Outputs summary of client stats for each radio in the following format "mac address, SNR, total data received,
# connection rate"
# Example output:
# iso.3.6.1.4.1.14125.2.1.1.15.9.0 = STRING: "cc:aa:bb:dd:ee:ff -56 445942Kb 105M,"
# iso.3.6.1.4.1.14125.2.1.1.16.9.0 = STRING: "aa:bb:cc:dd:ee:ff -64 18388740Kb 78M, bb:bb:cc:dd:ee:ff -63 77624Kb 44M,"
#
# To use this script,
# 1. Install bc package on OpenWRT/LEDE
# 2. Install snmpd package on OpenWRT/LEDE
# 3. Copy this file to a suitable directory on the openwrt device (e.g. mounted usb drive or /root) and make executable.
# 4. Modify UCI configuration file for snmpd in /etc/config/snmpd to include the following:
# config pass
# option miboid .1.3.6.1.4.1.14125.2.1.1
# option prog /root/wl_snmpd.sh (change this to match the path you placed the file in)
# option persist 1
# 5. Reboot device.
#
place=".1.3.6.1.4.1.14125.2.1.1"
refresh() {
id=1
ath1_clients=''
for interface in $(ifconfig | grep wlan1 | cut -d" " -f1)
do
ath1_clients=$ath1_clients' '$(iw dev $interface station dump)
done
mib51=""
for mac in $(echo $ath1_clients | grep -o -E '([a-z0-9]+):([a-z0-9]+):([a-z0-9]+):([a-z0-9]+):([a-z0-9]+):([a-z0-9]+)')
do
if test $id -gt 1
then
mib51=$mib51' '
fi
signal=$(echo $ath1_clients | grep -o -E 'signal avg: -([0-9]*)' | sed -n ${id}p | cut -d" " -f3)
tx_bytes=$(echo $ath1_clients | grep -o -E 'tx bytes: ([0-9]*)' | sed -n ${id}p | cut -d" " -f3)
tx_bytes=`echo "$tx_bytes/1000" | bc`
bitrate=$(echo $ath1_clients | grep -o -E 'rx bitrate: ([0-9]*)' | sed -n ${id}p | cut -d" " -f3)
let id=$id+1
result=$mac' '$signal' '$tx_bytes'Kb '$bitrate'M,'
mib51=$mib51$result
done
id=1
ath0_clients=''
for interface in $(ifconfig | grep wlan0 | cut -d" " -f1)
do
ath0_clients=$ath0_clients' '$(iw dev $interface station dump)
done
mib24=""
for mac in $(echo $ath0_clients | grep -o -E '([a-z0-9]+):([a-z0-9]+):([a-z0-9]+):([a-z0-9]+):([a-z0-9]+):([a-z0-9]+)')
do
if test $id -gt 1
then
mib24=$mib24' '
fi
signal=$(echo $ath0_clients | grep -o -E 'signal avg: -([0-9]*)' | sed -n ${id}p | cut -d" " -f3)
tx_bytes=$(echo $ath0_clients | grep -o -E 'tx bytes: ([0-9]*)' | sed -n ${id}p | cut -d" " -f3)
tx_bytes=`echo "$tx_bytes/1000" | bc`
bitrate=$(echo $ath0_clients | grep -o -E 'tx bitrate: ([0-9]*)' | sed -n ${id}p | cut -d" " -f3)
let id=$id+1
result=$mac' '$signal' '$tx_bytes'Kb '$bitrate'M,'
mib24=$mib24$result
done
value_136141141252111590=$mib24;
type_136141141252111590='string';
value_136141141252111690=$mib51;
type_136141141252111690='string';
getnext_13614114125211="$place.15.9.0"
getnext_136141141252111590="$place.16.9.0"
getnext_136141141252111690="NONE"
}
refresh
LASTREFRESH=0
while read CMD
do
case "$CMD" in
PING)
echo PONG
continue
;;
getnext)
read REQ
let REFRESH=$(date +%s)-$LASTREFRESH
if test $REFRESH -gt 14
then
LASTREFRESH=$(date +%s)
refresh
fi
oid=$(echo $REQ | tr -d .)
eval ret=\$getnext_${oid}
if test "x$ret" = "xNONE"
then
echo NONE
continue
fi
;;
get)
read REQ
let REFRESH=$(date +%s)-$LASTREFRESH
if test $REFRESH -gt 14
then
LASTREFRESH=$(date +%s)
refresh
fi
if test "x$REQ" = "x$place"
then
echo NONE
continue
else
ret=$REQ
fi
;;
*)
read REQ
if test "x$REQ" = "x$place"
then
echo NONE
continue
else
ret=$REQ
fi
;;
esac
oid=$(echo $ret | tr -d .)
if eval test "x\$type_${oid}" != "x"
then
echo $ret
eval echo "\$type_${oid}"
eval echo "\$value_${oid}"
else
echo NONE
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment