Junos Automation (Scripting)
Junos Automation (Scripting)

Help with script that set some ospf interfaces passive

[ Edited ]
2 weeks ago

Hi all,

i want to implement op script with slax, thats set passive all gr-* interfaces in ospf area 0. My script code

version 1.0;
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 str = "http://exslt.org/strings";
import "../import/junos.xsl";

match / {
        <op-script-results> {
                /* JUNOS XML API Element to retrieve the configuration */
                        var $config-rpc =  <get-configuration database="committed" inherit="inherit"> {
                                                <configuration> {
                                                <protocols> {
                                                <ospf>;
                                                }
                                        }
                                }
                        
                        var $config = jcs:invoke( $config-rpc );
                        
                        var $ospf-config = $config/protocols/ospf/area;

                        var $ospf-area := {
                                <configuration> {
                                        <protocols> {
                                                <ospf> {
              for-each($ospf-config[name == "0.0.0.0"]/interface[starts-with(name, "gr-")]) {
<passive> ; } } } } } var $connection = jcs:open(); var $results := { call jcs:load-configuration( $connection, $configuration = $ospf-area); } if( $results//xnm:error ) { for-each( $results//xnm:error ) { <output> message; } } } }

But script does not work, here example output

root> op test4                                                      
syntax error

root> 

Please help. What i am doing wrong?

 

3 REPLIES 3
Junos Automation (Scripting)
Solution
Accepted by topic author alex2410
2 weeks ago

Re: Help with script that set some ospf interfaces passive

[ Edited ]
2 weeks ago

Hello,

 

Here is the working script for You:

 

version 1.0;

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 str = "http://exslt.org/strings";

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

main <op-script-results> {
    /* JUNOS XML API Element to retrieve the configuration */
    var $config-rpc = <get-configuration database="committed" inherit="inherit"> {
        <configuration> {
            <protocols> {
                <ospf>;
            }
        }
    }
    var $config = jcs:invoke($config-rpc);
    var $ospf-config = $config/protocols/ospf/area;
    var $ospf-ifl-list := {
        for-each ($ospf-config[name == "0.0.0.0"]/interface[starts-with(name, "gr-")]) {
            <interface> {
                <name> ./name;
                <passive>;
            }
        }
    }
    var $ospf-config2 := <configuration> {
        <protocols> {
            <ospf> {
                <area> {
                    <name> "0.0.0.0";
                    copy-of $ospf-ifl-list;
                }
            }
        }
    }
    var $connection = jcs:open();
    var $results := {
        call jcs:load-configuration($connection, $configuration = $ospf-config2);
    }
    
    if ($results//xnm:error) {
        for-each ($results//xnm:error) {
            <output> message;
        }
    }
}

 

General comments:

1/ use jcsSmiley Surprisedpen() and jcs:execute() instead of jcs:invoke() - this is best practice

2/ do private commit with comment instead of default shared commit - again, this is best practice

3/ You missed quite a lot of XML tags to be wrapped around Your interface list

4/ consider what to do if all gr-* interfaces are already passive - You don't want to commit an empty config because it will eat up a slot in 50 recent commits limit that are kept by default.

5/ consider what to do if Your prepared interface list is empty - this can happen if someone removed gr-* interfaces from under [protocols ospf area 0], or removed [protocols ospf area 0], or removed [protocols ospf] - I hope You get my drift Smiley LOL

HTH

Thx

Alex

_____________________________________________________________________

Please ask Your Juniper account team about Juniper Professional Services offerings.
Juniper PS can design, test & build the network/part of the network as per Your requirements

+++++++++++++++++++++++++++++++++++++++++++++

Accept as Solution = cool !
Accept as Solution+Kudo = You are a Star !
Junos Automation (Scripting)

Re: Help with script that set some ospf interfaces passive

[ Edited ]
2 weeks ago

Yes, your code is working, except i needed to replace your string:

main <op-script-results> {

with my:

match / { <op-script-results> {

 

About which tags you are say in 3/ ?

 

Anyway, thank you very much man for your help and suggestions, You are great!

Junos Automation (Scripting)

Re: Help with script that set some ospf interfaces passive

[ Edited ]
2 weeks ago

Hello,

 


@alex2410 wrote:

 

About which tags you are say in 3/ ?

 


 

Your script outputs only these tags:

 

 <configuration> {
          <protocols> {
                  <ospf> {

 

- and then Your script adds 1 <passive> tag for each gr* interface. 

You need to add 3 more tag layers for the resulting config to be valid:

 

 <configuration> {
          <protocols> {
                  <ospf> {
                          <area> {
				                                       <interface> {
						                                               <name> 
						<passive>;

HTH

Thx

Alex

 

 

_____________________________________________________________________

Please ask Your Juniper account team about Juniper Professional Services offerings.
Juniper PS can design, test & build the network/part of the network as per Your requirements

+++++++++++++++++++++++++++++++++++++++++++++

Accept as Solution = cool !
Accept as Solution+Kudo = You are a Star !