Discussion:
[j-nsp] Is it possible to pass apostrophe character(ASCII dec code 39) as an argument value to SLAX script?
Martin T
2018-07-12 08:00:12 UTC
Permalink
Hi!

Is it possible to pass apostrophe character(ASCII dec code 39) as an
argument value to SLAX script? I have tried to escape it, but it does
not seem to work:

***@vmx1> op test chr '
''':(null):(2) Invalid expression
error: runtime error
error: Evaluating user parameter chr failed

***@vmx1> op test chr "'"
''':(null):(2) Invalid expression
error: runtime error
error: Evaluating user parameter chr failed

***@vmx1> op test chr \'
'\'':(null):(3) Invalid expression
error: runtime error
error: Evaluating user parameter chr failed

***@vmx1> op test chr '''
''''':(null):(2) Invalid expression
error: runtime error
error: Evaluating user parameter chr failed

***@vmx1>


Script named test is following:

***@vmx1> file show /var/db/scripts/op/test.slax
version 1.1;

ns junos = "http://xml.juniper.net/junos/*/junos";
ns xnm = "http://xml.juniper.net/xnm/1.1/xnm";
ns jcs = "http://xml.juniper.net/junos/commit-scripts/1.0";

import "../import/junos.xsl";

var $arguments = {
<argument> {
<name> "chr";
}
}

param $chr;

match / {
<op-script-results> {
<output> "Character: " _ "'" _ $chr _ "'";
}
}

***@vmx1>

I guess it is not possible and one needs to use get-input() function?


thanks,
Martin
_______________________________________________
juniper-nsp mailing list juniper-***@puck.nether.net
https://puck.nether.net/mailman/listinfo/juniper-nsp
Phil Shafer
2018-07-12 20:21:06 UTC
Permalink
Post by Martin T
Is it possible to pass apostrophe character(ASCII dec code 39) as an
argument value to SLAX script? I have tried to escape it, but it does
Quote it:

version 1.2;

param $char = "-";

main <op-script-results> {
<output> "got: " _ $char;
}

***@box> op apos char "`"
got: `

***@box>

Thanks,
Phil
_______________________________________________
juniper-nsp mailing list juniper-***@puck.nether.net
https://puck.nether.net/mailman/listinfo/juniper-nsp
Martin T
2018-07-12 21:03:48 UTC
Permalink
Post by Phil Shafer
Post by Martin T
Is it possible to pass apostrophe character(ASCII dec code 39) as an
argument value to SLAX script? I have tried to escape it, but it does
version 1.2;
param $char = "-";
main <op-script-results> {
<output> "got: " _ $char;
}
got: `
Thanks,
Phil
Phil,

aren't you using grave accent("echo -e "\x60"") character? I was using
"echo -e "\x27"" character.


thanks,
Martin
_______________________________________________
juniper-nsp mailing list juniper-***@puck.nether.net
https://puck.nether.net/mailman/listinfo/juniper-nsp
Phil Shafer
2018-07-12 21:35:31 UTC
Permalink
Post by Martin T
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:

***@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
_______________________________________________
juniper-nsp mailing list juniper-***@puck.nether.net
https://puck.nether.net/mailman/listinfo/juniper-nsp
Martin T
2018-07-17 11:15:15 UTC
Permalink
Post by Phil Shafer
Post by Martin T
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.
''':(null):(2) Invalid expression
error: runtime error
error: Evaluating user parameter char failed
% slaxproc -E -n cs-examples/apos.slax -g -a char "'"
<?xml version="1.0"?>
<op-script-results>
<output>got: '</output>
</op-script-results>
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:

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

***@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
_______________________________________________
juniper-nsp mailing list juniper-***@puck.nether.net
https://puck.nether.net/mailman/listinfo/juniper-nsp

Loading...