Junos Automation (Scripting)
Junos Automation (Scripting)

Bad code or Bug?

‎01-06-2017 10:53 PM

 

Hello,

 

I expierenced strange results in an event script (EX3300 @ 15.1R5) where one specific variable did have a value (expected behavior) but often not (unexpected behavior).

After some debugging I was able to verify the problem with the following little script:

 

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";
ns xutil extension = "http://xml.libslax.org/xutil";


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

var $xml-content := xutil:string-to-xml(slax:document("/var/tmp/any-xml-file.xml"));
var $severity = {
    if ($xml-content) {
        expr 4;
    } else {
        expr 1;
    }
}

match / {
    <op-script-results> {
        copy-of ($xml-content);
    }
}


The file /var/tmp/any-xml-file.xml is not changing, but often the variable $xml-content seems to be empty and the script fails with the following message:

# run op op-test | display xml    
<rpc-reply xmlns:junos="http://xml.juniper.net/junos/15.1R5/junos">
    <xnm:error xmlns="http://xml.juniper.net/xnm/1.1/xnm" xmlns:xnm="http://xml.juniper.net/xnm/1.1/xnm">
        <message>
            op script fails with exit code 0x8b
        </message>
    </xnm:error>
    <cli>
        <banner>{master:0}[edit]</banner>
    </cli>
</rpc-reply>

 

 If I remove the following part, the script then runs fine every time and $xml-content has the desired data.

var $severity = {
    if ($xml-content) {
        expr 4;
    } else {
        expr 1;
    }
}

 

My question is, is this a bug, or is there something wrong with this code part?

 

Thanks a lot .

 

 

Kind Regards,

Leon Kramer

 

1 REPLY 1
Junos Automation (Scripting)

Re: Bad code or Bug?

‎01-06-2017 11:41 PM

Maybe this is a beginners mistake. The scripts works flawlessly if the problematic part is moved into the match / { .. } part.

 

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";
ns xutil extension = "http://xml.libslax.org/xutil";

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

var $xml-content := xutil:string-to-xml(slax:document("/var/tmp/any-xml-file.xml"));

match / {
    var $severity = {
        if ($xml-content) {
            expr 4;
        } else {
            expr 1;
        }
    }
    <op-script-results> {
        copy-of ($xml-content);
    }
}