[j-nsp] SLAX script, redefining variables

Phil Shafer phil at juniper.net
Fri Jun 7 12:22:58 EDT 2013


Tom Storey writes:
>It seems that older SLAX implementations dont have the ability to redefine
>variable (Juniper is calling them immutable variables).

Not my term; it's a limited of XSLT, on which SLAX is based.

Some details (and implementation notes on the new mvars) can
be found here:

http://code.google.com/p/libslax/wiki/ImmutableVariables

>This is apparently
>fixed in 1.1 on JunOS12+ boxes with something called a mutable variable
>(defined with mvar instead of var) but all of the boxes I am using are not
>on JunOS12+ yet.

SLAX-1.1 is in 12.2.  You can also get it (for offline use) from
libslax.googlecode.com and juise.googlecode.com.  libslax is the
base language.  juise adds JUNOS-specific bits like the jcs:*
functions.

[Yes, we're in the process of moving to github, but not done yet]

>Has anyone found a way to collect a whole bunch of information, and then
>display it at the end of the script, rather than duplicating their output
>code 50 times to handle the various exceptions?

I'm not quite following; can you share your script?

>e.g. a script I am writing grabs the tx/rx figures for optics and lists
>them, along with the description of the interface, so I can run my op
>script and get a list of power levels, grep for destination devices etc and
>see whats happening with my circuits.

>But there are a couple of exceptions that I hit along the way:
>
>1) Descriptions arent always on the physical interface, so if theres no
>description there I need to look at a logical interface, which is usually
>unit 0 in my case

You can put logic inside the "var", like:

    var $desc = {
        if (description) {
            expr description;
        } else if (../description) {
            expr ../description;
        } else {
            expr "--";
        }
    }

For this specific case, there's the jcs:first-of() function to
shorten this:

    var $desc = jcs:first-of(description, ../description, "--");

where jcs:first-of returns the first non-null argument.

But you can use this like:

    var $links := {
        for-each (link) {
            <link> {
               <name> name;
               <description> jcs:first-of(description, ../description, "--");
               /* ... */
            }
        }
    }

to build custom data structures.

Also there's always the original model from XSLT of apply-templates
and recursive traversal:

http://my.safaribooksonline.com/book/xml/0596003722/selecting-and-traversing/xsltckbk-chp-4-sect-7

>2) SONET interfaces dont use the same "variable" to store the rx power
>figure as ethernet interfaces, so depending on the interface type I need to
>look at a different variable for the rx power figure

    var $power = {
        if (type == "aa") {
            expr where/ever/it/is/stored;
        } else if (type == "bb") {
            expr some/where/else;
        } else {
            expr "-";
        }
    }

Hope this helps......

Thanks,
 Phil



More information about the juniper-nsp mailing list