Automation

Expand all | Collapse all

Help with script that set some ospf interfaces passive

Jump to Best Answer
  • 1.  Help with script that set some ospf interfaces passive

    Posted 11-23-2019 10:11

    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?

     



  • 2.  RE: Help with script that set some ospf interfaces passive
    Best Answer

    Posted 11-23-2019 21:09

    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 jcs:open() 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



  • 3.  RE: Help with script that set some ospf interfaces passive

    Posted 11-24-2019 02:17

    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!



  • 4.  RE: Help with script that set some ospf interfaces passive

    Posted 11-24-2019 02:30

    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