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!

Solved GNP NVRAM setting question and Script advice

kstamand

Regular Contributor
Looking for scripting advice on how best to parse the Guest Network Pro (GNP) nvram vlan_rl value??

nvram vlan_rl value question
My setup has 3 custom GNP VLAN where the nvram setting vlan_rl has the value <1>52>0><2>53>0><3>54>0>
My understanding is that vlan_rl consists of columns of information >> SDN # (1, 2, or 3 in my case), Bridge Interface number (52, 53, or 54), and I'm not sure what the third field is for (0)
Question 1 - what is the third column of information in the nvram setting vlan_rl in each of the three SDN (0 in my case)?

Script question/advice, related to the nvram vlan_rl setting
My understanding is that the SDN number can be 1 - 2 characters, as can the Bridge Interface number, and I'm going to assume that third value can be 1 - 2 characters as well. I also understand there can be a variable number of SDN (1 - 9 or more)
Question 2 (I'm not sure how best to ask, but ..), is there some pattern matching magic that allows me to dynamically loop through the nvram vlan_rl string and pull out each SDN combination of information (SDN #, Bridge Interface #, third ??) into an array or separate variables - Something like SDN#: value, BR#: value, XX#, value??

Goal is to have a "somewhat" dynamic DNSMASQ-SDN.POSTCONF for dnsmasq customizations
Currently, my dnsmasq-sdn.postconf has hard coded SDN# references for adding different domain names (and potential other information) for each VLAN (e.g., vlan 1 = home.lan, vlan 2 = adults.lan, ...) and it's working well.
I'm just looking to enhance my scripting skills.
 
Use the get_mtlan command to see how the nvram is broken up into relatable fields.
 
Not sure if this is helpful, but when I needed the bridge identifier in a dnsmasq sdn post conf script, I used this kludge:
Bash:
BR=$(nvram get subnet_rl | tr '<' '\n' | awk -F'>' '{print $1 ":" $2}' | grep $SDN":" | awk -F':' '{print $2}')

Where I had previously set the variable SDN to the second received parameter $2.
 
Use the get_mtlan command to see how the nvram is broken up into relatable fields.
Interesting command with useful information for future reference - thank you
 
Last edited:
Not sure if this is helpful, but when I needed the bridge identifier in a dnsmasq sdn post conf script, I used this kludge:
Bash:
BR=$(nvram get subnet_rl | tr '<' '\n' | awk -F'>' '{print $1 ":" $2}' | grep $SDN":" | awk -F':' '{print $2}')

Where I had previously set the variable SDN to the second received parameter $2.
Thanks for the suggestion!!
 
I ended up creating the following script to parse the nvram vlan_rl setting for my current needs, in case it may be helpful for others.

Code:
#!/bin/sh
# set -x # uncomment for debugging purposes

# read the list of vlan from nvram variable vlan_rl
vlan_list="$(nvram get vlan_rl)"

# count the number of vlan in the vlan_list string (each occurrence of the string "<" marks the beginning of a new set of vlan info)
vlan_count=$(echo $vlan_list | grep -o "<" | wc -l)

# get the length of the vlan_list string
vlan_list_length=${#vlan_list}

# calculate the length of each string of vlan info (sdn#, vlanid, ??) by dividing total vlan_list length by the number of vlan in list
let vlan_str_length=$vlan_list_length/$vlan_count

# set substring start position, for extracting individual vlan info set from vlan_list
substring_start=0

# loop through the vlan_list string, for the count of vlans identified
for i in 0 .. $vlan_count; do
    substring="${vlan_list:$substring_start:$vlan_str_length}" # extract a set of vlan info from the vlan_list string

    # strip out the '<' and '>' characters from the substring
    vlan_info="$(echo $substring | tr '<' ' ' | tr '>' ' ')"

    #now pick up each piece of information
    sdn_idx="$(echo $vlan_info | cut -f1 -d ' ')" # capture sdn index number
    vlan_id="$(echo $vlan_info | cut -f2 -d ' ')" # capture vlan id
    unknown_id="$(echo $vlan_info | cut -f3 -d ' ')" # capture unknown id

    echo "sdn_idx - $sdn_idx ; vlan_id - $vlan_id ; unknown_id - $unknown_id"

    #move the substring start position over to the next set of vlan info
    let substring_start=$substring_start+$vlan_str_length
done
 
Just a couple of observations:

...
#now pick up each piece of information
sdn_idx="$(echo $vlan_info | cut -f1 -d ' ')" # capture sdn index number
vlan_id="$(echo $vlan_info | cut -f2 -d ' ')" # capture vlan id
unknown_id="$(echo $vlan_info | cut -f3 -d ' ')" # capture unknown id
The 3rd parameter (i.e. unknown_id) in each VLAN entry from the NVRAM vlan_rl variable indicates the VLAN Port Isolation (Enabled=1, Disabled=0). IOW, each VLAN entry consists of:
Rich (BB code):
<SDN_Index>VLAN_ID>VLAN_Port_Isolation>

...
# calculate the length of each string of vlan info (sdn#, vlanid, ??) by dividing total vlan_list length by the number of vlan in list
let vlan_str_length=$vlan_list_length/$vlan_count
...
#move the substring start position over to the next set of vlan info
let substring_start=$substring_start+$vlan_str_length
The above code is incorrect because it assumes that all VLAN entries will always have equal length, which is not true for all possible scenarios. For example, the VLAN_ID can be between 1 to 4 digits (i.e. ID value between 1 to 4093).

My 2 cents.
 
Just a couple of observations:


The 3rd parameter (i.e. unknown_id) in each VLAN entry from the NVRAM vlan_rl variable indicates the VLAN Port Isolation (Enabled=1, Disabled=0). IOW, each VLAN entry consists of:
Rich (BB code):
<SDN_Index>VLAN_ID>VLAN_Port_Isolation>


The above code is incorrect because it assumes that all VLAN entries will always have equal length, which is not true for all possible scenarios. For example, the VLAN_ID can be between 1 to 4 digits (i.e. ID value between 1 to 4093).

My 2 cents.
Thank you for filling in the “unknown_id” blank for me and catching my vlan info length miscalculation. I’ll redo the code, unless you have an alternate suggestion on how to parse that type of sting + encoding(<sdn_index>vlan_id>vlan_port_isolation><..>..>..><..>..>..> ) ??
 
Thank you for filling in the “unknown_id” blank for me and catching my vlan info length miscalculation. I’ll redo the code, unless you have an alternate suggestion on how to parse that type of sting + encoding(<sdn_index>vlan_id>vlan_port_isolation><..>..>..><..>..>..> ) ??
Not sure if this is correct, but I view the "<" as a next row indicator and the ">" as a next column indicator.
 
Not sure if this is correct, but I view the "<" as a next row indicator and the ">" as a next column indicator.
That is my understanding as well. I just don’t know if it’s easier / clearer to attempt it the way I am In the sample code or if there is some parsing code technique I could be using (something like a JSON parser)
 
Last edited:
That is my understanding as well. I just don’t know if it’s easier / clearer to attempt it the way I am In the sample code or if there is some parsing code technique I could be using (something like a JSON parser)
I use this to make these variables human readable:
Code:
nvram get vlan_rl | tr '<' '\n' | tr '>' '\t'

You can use tricks with awk and grep to pick out the parts you are looking for.
 
I use this to make these variables human readable:
Code:
nvram get vlan_rl | tr '<' '\n' | tr '>' '\t'

You can use tricks with awk and grep to pick out the parts you are looking for.
That’s a hell of a lot easier - THANKS!!!
 
Thank you for filling in the “unknown_id” blank for me and catching my vlan info length miscalculation. I’ll redo the code, unless you have an alternate suggestion on how to parse that type of sting + encoding(<sdn_index>vlan_id>vlan_port_isolation><..>..>..><..>..>..> ) ??
Here's a simple function that parses the NVRAM value and iterates through each VLAN entry:
Bash:
_Extract_NVRAM_vlan_rl_INFO_()
{
    echo "VLAN_INDX,VLAN_ID,PORT_ISOLATION"
    while read -r VLAN_INDX VLAN_ID PORT_ISOLATION && [ -n "$VLAN_INDX" ]
    do  echo "${VLAN_INDX},${VLAN_ID},$PORT_ISOLATION"
    done <<EOT
$(nvram get vlan_rl | sed 's/></\n/g;s/</\n/g;s/>$//g;s/>/ /g' | sed '/^$/d')
EOT
}
You could modify the function to output the results to a temporary CSV file so other functions down the line can easily read and process the extracted information as needed.

HTH.
 
Here's a simple function that parses the NVRAM value and iterates through each VLAN entry:
Bash:
_Extract_NVRAM_vlan_rl_INFO_()
{
    echo "VLAN_INDX,VLAN_ID,PORT_ISOLATION"
    while read -r VLAN_INDX VLAN_ID PORT_ISOLATION && [ -n "$VLAN_INDX" ]
    do  echo "${VLAN_INDX},${VLAN_ID},$PORT_ISOLATION"
    done <<EOT
$(nvram get vlan_rl | sed 's/></\n/g;s/</\n/g;s/>$//g;s/>/ /g' | sed '/^$/d')
EOT
}
You could modify the function to output the results to a temporary CSV file so other functions down the line can easily read and process the extracted information as needed.

HTH.
Having a function, with results output to a file, was my end state goal of my initial code hack.

Interesting and educational for me, how this sample code accomplishes what I was attempting, but in a much more elegant fashion.

Thank you @Martinski, @dave14305, and @rung for all the tips and guidance. This forum and members like yourself are the best.
 
Having a function, with results output to a file, was my end state goal of my initial code hack.
In that case, here's a leaner version of the function that does exactly what you want and nothing else:
Bash:
# Global variable #
vlan_rl_CSVfilePath="$HOME/NVRAM_vlan_rl_INFO.CSV"

_Extract_NVRAM_vlan_rl_INFO_()
{
   {
      echo "VLAN_INDX,VLAN_ID,PORT_ISOLATION"
      nvram get vlan_rl | sed 's/></\n/g;s/</\n/g;s/>$//g;s/>/,/g' | sed '/^$/d'
   } > "$vlan_rl_CSVfilePath"
}
If you prefer, you could instead pass the CSV file path as a parameter to the function.,
 

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