[j-nsp] Is it possible to pass apostrophe character(ASCII dec code 39) as an argument value to SLAX script?

Martin T m4rtntns at gmail.com
Tue Jul 17 07:15:15 EDT 2018


On Fri, Jul 13, 2018 at 12:35 AM Phil Shafer <phil at juniper.net> wrote:
>
> Martin T writes:
> >aren't you using grave accent("echo -e "\x60"") character? I was using
> >"echo -e "\x27"" character.
>
> Doh!  I read apostrophe (even named the script apos.slax) but my
> brain turned into backtick.
>
> Yes, this looks like a JUNOS bug:
>
> root at box> op apos char "'"
> ''':(null):(2) Invalid expression
> error: runtime error
> error: Evaluating user parameter char failed
>
> The underlaying slax library handles it correctly:
>
> % slaxproc -E -n cs-examples/apos.slax -g -a char "'"
> <?xml version="1.0"?>
> <op-script-results>
>   <output>got: '</output>
> </op-script-results>
>
> But it looks like this is explicitly handled in slaxproc.c:
>
>             quote = strrchr(pvalue, '\"') ? '\'' : '\"';
>             tvalue[0] = quote;
>             memcpy(tvalue + 1, pvalue, plen);
>             tvalue[plen + 1] = quote;
>             tvalue[plen + 2] = '\0';
>
> This logic doesn't appear in the JUNOS driver (/usr/libexec/ui/cscript).
> I'll open a PR for this.
>
> There is a limitation in XSLT that one can't mix strings with both
> single and double quotes.  Strange but true.
>
> Thanks,
>  Phil

Thanks for confirming this! Just for information, another interesting
quirk I found is that in the case of non-interactive SSH mode, the ";"
character breaks the command on cli if it is preceded by escaped
double-quote character. I tested this with Net::OpenSSH, Net::SSH2 and
Net::SSH::Expect Perl modules, but one can demonstrate this easily
with OpenSSH client as well. For example:

$ ssh vmx1 'set cli directory "f\"oo;bar"'

error: Cannot set directory to 'f"oo'

error: unknown command: bar
$

In interactive mode, there is no such limitation:

martin at vmx1> set cli directory "f\"oo;bar"
error: Cannot set directory to 'f"oo;bar'

martin at vmx1>



Last problematic printable(dec 32 to dec 126) ASCII character, which I
found, was "\". It seems to work in a way that it escapes the
double-quote character('\"' becomes '"') and reports a syntax error if
"\" isn't followed by any other character.


The reason I found those Junos cli quirks is that I wrote a test
script(https://perldoc.perl.org/Test/More.html), which among other
things, generates a random string of characters from " "(dec 32) to
"~"(dec 126) which becomes an argument value for op script. So in
order to avoid any Junos cli errors, I'm doing following substitutions
to this randomly generated string before returning it to Perl
Net::SSH::Expect module:

# Replace one or more \ character(s) with single _ if the \
character(s) is at the end of the string.
$str =~ s/\\+$/_/;
# Delete all the \ character(s) if they are preceeding a " character.
$str =~ s/\\+"/"/g;
# Delete all ' characters.
$str =~ s/'/_/g;
# Escape the " character.
$str =~ s/"/\\"/g;

return $str;

I'm not saying, that this is perfect, but it is best I have managed to
come up with. I try to preserve the rendomly generated string as much
as possible. As much as I have tested, it seems to work.


regards,
Martin


More information about the juniper-nsp mailing list