What's new
  • SNBForums Code of Conduct

    SNBForums is a community for everyone, no matter what their level of experience.

    Please be tolerant and patient of others, especially newcomers. We are all here to share and learn!

    The rules are simple: Be patient, be nice, be helpful or be gone!

helper/tool scripts

thesilence

New Around Here
Hi, my apologies if I'm in the wrong section. I'm wondering if there is a repository of helper scripts to parse common (or not so common) nvram settings and output them in an easy to parse way for larger scripts, or just for quick command-line reference? A quick search says no but I'm hopeful :) Assuming there isn't, here is my first jab at something useful (feel free to use/modify/throw out the window), or add your own to the thread if you like.

Code:
#!/bin/sh
# apg_info.sh
# print entry, index, bridge, vlan, vlan rule, enabled, ssid for each guest network
printf "Entry#\tIdx#\tBridge\tVLAN#\tVLANRul\tEnabled\tSSID\n"
for i in 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18; do
  SSID=`nvram get apg$i'_'ssid`
  if [ "$SSID" != "" ]; then
    IDX=`nvram get apg$i'_'security|sed 's/^.*[^0-9]\([0-9]*\)$/\1/'`
    VL=`nvram get apg_br_info | tr '<' '\n' | awk -F'>' '{print $1 ":" $2}'|grep "$i:"|cut -f1 -d':'`
    BRG=`nvram get apg_br_info | tr '<' '\n' | awk -F'>' '{print $1 ":" $2}'|grep "$i:"|cut -f2 -d':'`
    EN=`nvram get apg$i'_'enable |sed 's/1/y/g; s/0/n/g'`
    VLR=`nvram get vlan_rl|tr '<' '\n'|awk -F'>' '{print $1 ":" $2}'|grep ":$VL"|cut -f1 -d':'`
    if [ "$VL" == "" ]; then VL="-";VLR="-";fi
    if [ "$BRG" == "" ] && [ "$EN" == "y" ]; then BRG="br0";fi
    printf "$i\t$IDX\t$BRG\t$VL\t$VLR\t$EN\t$SSID\n"
fi
done
 
Hi, my apologies if I'm in the wrong section. I'm wondering if there is a repository of helper scripts...
The tacked/sticked Asuswrt-Merlin Addon Software Catalog at the top of this subforum has a listing of the main scripts discussed in this subforum. Beyond that people sometimes post code to accomplish specific things to this subforum.
 
There's also some guides (and a few small code examples) on the asuswrt-merlin wiki that may help or give you some ideas
 
FYI,

There are a couple of errors in the script.

...
IDX=`nvram get apg$i'_'security|sed 's/^.*[^0-9]\([0-9]*\)$/\1/'`
VL=`nvram get apg_br_info | tr '<' '\n' | awk -F'>' '{print $1 ":" $2}'|grep "$i:"|cut -f1 -d':'`
BRG=`nvram get apg_br_info | tr '<' '\n' | awk -F'>' '{print $1 ":" $2}'|grep "$i:"|cut -f2 -d':'`
...
The above 2 lines (VL=... and BRG=...) are incorrect because they're searching and extracting data based on the index rather than the VLAN ID, so it can result in finding the wrong interface name (e.g., brxy) associated with the VLAN ID.

Here's a function showing one way to fix the errors:
Bash:
_Get_APG_Info_()
{
   printf "Entry  INDX  Bridge  VLAN_ID  VLAN_Index  Enabled  SSID\n"
   for num in 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
   do
       SSID="$(nvram get "apg${num}_ssid")"
       [ -z "$SSID" ] && continue
       INDX="$(nvram get "apg${num}_security" | sed 's/^.*[^0-9]\([0-9]*\)$/\1/')"
       VIDX="$(nvram get vlan_rl | tr '<' '\n' | awk -F'>' '{print $1 ":" $2}' | grep "^${num}:" | cut -d':' -f1)"
       VLAN="$(nvram get vlan_rl | tr '<' '\n' | awk -F'>' '{print $1 ":" $2}' | grep "^${num}:" | cut -d':' -f2)"
       BRGE="$(nvram get apg_br_info | tr '<' '\n' | awk -F'>' '{print $1 ":" $2}' | grep "^${VLAN}:" | cut -d':' -f2)"
       ENBL="$(nvram get "apg${num}_enable" | sed 's/1/Yes/g; s/0/No/g')"
       if [ -z "$VLAN" ]; then VLAN="-"; VIDX="-" ; fi
       if [ -z "$BRGE" ] && [ "$ENBL" = "Yes" ]; then BRGE="br0" ; fi
       printf "%3s%6s%5s%-4s%4s%-4s%8s%-3s%6s%-3s%5s%s\n" "$num" "$INDX" '' "$BRGE" '' "$VLAN" '' "$VIDX" '' "$ENBL" '' "$SSID"
   done
}
Basically, the solution is to find the VLAN ID first to get the associated interface name.
 
There are a couple of errors in the script.

The above 2 lines (VL=... and BRG=...) are incorrect because they're searching and extracting data based on the index rather than the VLAN ID, so it can result in finding the wrong interface name (e.g., brxy) associated with the VLAN ID.

Here's a function showing one way to fix the error...
Basically, the solution is to find the VLAN ID first to get the associated interface name.
Cheers & thankyou @Martinski your version is a huge improvement

edit: actually, on my rt-be92u the output is wrong with your function, perhaps because br0 has no vlan assignment in vlan_rl. I'll do a little investigationing.
 
Last edited:
edit: actually, on my rt-be92u the output is wrong with your function, perhaps because br0 has no vlan assignment in vlan_rl. I'll do a little investigationing.
AFAIK, based on some recent work I've been doing with the YazDHCP add-on, by default, the configuration for 'br0' (Main LAN) does not have a VLAN associated with it in the vlan_rl variable, so that should not be a problem per se, but without getting any more details and context, I don't know what you're actually seeing as output.
 

Latest threads

Support SNBForums w/ Amazon

If you'd like to support SNBForums, just use this link and buy anything on Amazon. Thanks!

Sign Up For SNBForums Daily Digest

Get an update of what's new every day delivered to your mailbox. Sign up here!
Back
Top