SHELL Scripting Help

I would like to call out to any SH scripting Gurus out there for some assistance.

During my time messing with my WiFi Router, I managed to created some rogue variables in NVRAM that I can’t seem to get rid of, short of doing a full NVRAM CLEAR and starting over setting up my Router. It’s not that big of a deal to do so, but I want to see if I can fix the problem without clearing NVRAM. Let’s see what we have…

# nvram show | grep wl0.1_wpa_psk
# nvram show | grep "wl0.1_wpa_psk" | grep "SETTING"

The bad string is represented by the following, where I’ve used the exclamation (!) as a placeholder for some embedded invisible character(s). The ! may be 1 character, or 2 or 3, or more - I don’t know.

             ^                 ^^

What I’m trying to accomplish is this:

nvram unset wl0.1_wpa_psk!

So my methodology is to attempt to capture the bad character(s) into a variable such as $badchars, then do:

nvram unset "wl0.1_wpa_psk$badchars"

This is where I need the help. How do I capture the bad character(s) when I don’t know the length, using for example sed?

badstring=$(nvram show | grep "wl0.1_wpa_psk" | grep "SETTING")
badchars=$(echo $badstring | sed 's/wl0.1_wpa_psk//g')

At this point $badchars should contain !={SETTING:WiFiPW}!!, but we’re not done. I want to filter it down to the leading ! and get rid of all the rest.

Not knowing the character count of the leading ! (the character(s) before =), I’m stuck. Maybe I should use awk at this point to split on = and take the first array element… having trouble with the syntax.

I think I have it:

badpart=$(echo $badchars | awk '{split($0,bad,"=")} END{print bad[1]}')

Yes, I got it!

For Guest Network 1:

# nvram show | grep wl0.1_wpa_psk

# badstring=$(nvram show | grep "wl0.1_wpa_psk" | grep "SETTING")
# badchars=$(echo $badstring | sed 's/wl0.1_wpa_psk//g')
# badpart=$(echo $badchars | awk '{split($0,bad,"=")} END{print bad[1]}')
# nvram unset "wl0.1_wpa_psk$badpart"
# nvram commit
# nvram show | grep wl0.1_wpa_psk
# nvram show | grep "SETTING"

@QMcKay Where those ! characters coming from?

@emre, they came from experimenting with sending shell script commands through PLINK. I can elaborate further if you wish, however…

It is not an issue in SambaPOS - they were not caused by SambaPOS. :wink:

OK. New line command for ESC/POS translates to ! char so I thought if it relates with that or not. Thanks for clarification.

btw did you started using it on production? How it works?

I am currently using it in production, and it works great! :smiley:

Great! I found that lib while looking for plink. I’ll test it when I have free time :slight_smile:

Do you think releasing such functionality as an add on module will have benefits over using plink?

1 Like

It will have a benefit only if you can feed it an entire .sh script resident on the client and have it execute line-by-line on the host, returning result codes for each line.

The issue with plink (or PuTTY) is that it doesn’t really accept or execute a full script that resides on the client. That’s why I ended up messing up the NVRAM in my router. FYI, I also tried the community edition of Bitvise SSH Client and its command-line tools sexec and stermc, with the same result.

The issue might be attributed to the difference in CRLF between Windows and *nix shells - I’m not sure.

The issues I ran in to were this:

plink.exe -ssh %rHOST% -P %rPORT% -l %rUSER% -pw %rPASS% -m "%rSCRIPT%" > %LOGFILE% 2>&1

In the above command, the -m switch accepts commands, either in-line, or via a script. When you pass in a script that is larger than a certain number of lines/characters, plink will report an error that the “server refused to execute a connection or a command”. I’m not sure what the exact limit is, but it isn’t very large. Again, it may be due to plink sending the script as a single concatenated command, where it misinterprets the CRLF at the end of each script line.

plink.exe -v -ssh %rHOST% -P %rPORT% -l %rUSER% -pw %rPASS% $SHELL< "%rSCRIPT%" > %LOGFILE% 2>&1

The method above will successfully pass the entire script to the host, but again, it believe plink is sending the script as a single concatenated command, where it misinterprets the CRLF at the end of each script line.

And therein lies the issue that messed up my NVRAM. Imagine executing the following as a single line (with Windows CRLF characters embedded all over the place within the line)… the results could be disastrous (and partially were in my case), and certainly unpredictable:

echo ---
echo Shell Script BEG `date +'%Y-%m-%d_%H.%M.%S'`
echo ---
echo arg1 $arg1
echo n24_1 $guestnet24_1
echo n50_1 $guestnet50_1
echo p24_1 $guestpwd24_1
echo p50_1 $guestpwd50_1
echo ---
gn1=`nvram show | grep wl..._wpa_psk | sort`
echo $gn1 | sed 's/ /\r\n/g'
echo ---
echo nvram set $guestnet24_1=$guestpwd24_1 ...
nvram set $guestnet24_1=$guestpwd24_1
echo ---
echo nvram set $guestnet50_1=$guestpwd50_1 ...
nvram set $guestnet50_1=$guestpwd50_1
echo ---
echo nvram commit ...
nvram commit

The issue as I see it, is that these tools were meant to execute (single) simple commands (which obviously must be available on the host), or execute a script that resides on the host. But they were not designed to execute a script that resides on the client.

I am just wondering why you don’t copy that script to the router and then call it with those arguments?

1 Like

I’ve considered doing doing just that @neko. :smile:

It wouldn’t be a big deal for me to do so, but the solution I was going for was to not have that as a requirement, so that it would be a little bit more universal.

I can agree that there are some slightly more advanced techniques going on in my Tutorial, but I was attempting to keep it as simple as possible, without getting into discussions of how to upload script A to Router B of brand C revision D running software E, and where to put it, and make it executable, etc.

What I’ve posted as a final solution in the Tutorial is fairly bullet-proof, and is relatively simple to edit, considering it is only a single .bat file. Though I am somewhat disappointed that I wasn’t successful using a Printer Template as a script, but c’est la vie. :wink:

Any idea how to get return value from Start Process batch file

Most commands executed in a command shell will return a result code. This is fine for logging to a text file that you could later review for errors.

However, the return value is not fed back to SambaPOS via the Start Process Action, unfortunately. I have requested the addition of this feature in the past, but even v5 does not (yet) support reading return values.

1 Like

@QMcKay I can’t believe I’ve ignored one of your request lol. I really don’t have something related with that. Maybe it will be better to implement a JS based solution for V5. I’ll note that.

1 Like

I’ve implemented Start Process JS Helper for V5.1.51.
You’ll find detailed info when 5.1.51 released.