Automation

 View Only
last person joined: 11 days ago 

Ask questions and share experiences about Apstra, Paragon, and all things network automation.
  • 1.  Help with script to change route metric in ospf

    Posted 01-07-2016 07:27

    Hi all,

     

    I have created this simple script to change the route metric of ospf interface.

     

    root# show protocols ospf
    area 0.0.0.0 {
        interface ge-0/0/1.0;
        interface ge-0/0/0.0 {
            passive;
            metric 100;
        }
    }

     

    But its not working. no erro but not solving the problem.

     

     

    root@% ls
    test.slax
    root@% vi test.slax
    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";
    
    
    import "../import/junos.xsl";
    match / {
    /* The configuration change must be defined */
    var $configuration-change = <configuration> {
                    < protocol> {
                      <ospf> {
                         <area> {
                           <name> "0.0.0.0";
                           <interface> {
                             <name> "ge-0/0/0";
                             <metric>  {
                               <name> "10";
                              }
                            }
                         }
                      }
                    }
                  }
    
    var $connection = jcs:open();
    
    var $results := { call jcs:load-configuration( $connection, $configuration = $configuration-change ); }
    if( $results//xnm:error ) {
      for-each( $results//xnm:error ) {
        <output> message;
      }
    }
    
    var $close-results = jcs:close($connection);
    }
    

    i can see its executing

     

    d[1759]: PING_TEST_FAILED: pingCtlOwnerIndex = test, pingCtlTestName = t1
    
    Message from syslogd@ at Jan  7 06:34:11  ...
     rmopd[1759]: PING_PROBE_FAILED: pingCtlOwnerIndex = test, pingCtlTestName = t1
    
    Message from syslogd@ at Jan  7 06:34:11  ...
     eventd[868]: EVENTD_ESCRIPT_EXECUTION: Trying to execute the script 'test.slax' from '/var/db/scripts/event/'
    
    Message from syslogd@ at Jan  7 06:34:11  ...
     rmopd[1759]: PING_TEST_FAILED: pingCtlOwnerIndex = test, pingCtlTestName = t1

     

    but not doing what it suppose to do that chaning the metric.

      OSPF database, Area 0.0.0.0
     Type       ID               Adv Rtr           Seq      Age  Opt  Cksum  Len
    Router  *10.0.2.26        10.0.2.26        0x8000000a  2007  0x22 0x37b   48
      bits 0x0, link count 2
      id 192.168.1.0, data 255.255.255.0, Type Stub (3)
        Topology count: 0, Default metric: 100
      id 172.16.2.2, data 172.16.2.1, Type Transit (2)

     

    platform : vsrx

     

    Model: firefly-perimeter
    JUNOS Software Release [12.1X47-D10.4]
    

     

     

    Thanks alot


    #routemetric


  • 2.  RE: Help with script to change route metric in ospf

     
    Posted 01-08-2016 06:07

    Hi,

     

    I think that there are a number of issues here that are preventing you from completing this task.

     

    1.  In order for the <output> message; statement to display something on screen, you first need to wrap it with an <op-script-results> tag (for an op script), or <event-script-results> (for an event script) etc.

     

    2.  In your output the ns statements appear incorrect, "junos" rather than "Junos".

    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";

     

    3.  Finally, $configuration-change should be corrected, as the configuration is invalid for the metric.

        var $configuration-change = <configuration>{
          <protocols> {
            <ospf> {
              <area> {
                <name> "0.0.0.0";
                <interface> {
                  <name> "ge-0/0/0";
                  <metric> "10";
                }
              }
            }
          }
        }
    

    With these changes applied, you should be able to push the configuration.

     

    Regards,

    Andy

     

     



  • 3.  RE: Help with script to change route metric in ospf

    Posted 01-11-2016 02:03

     

    here is the update code.

     

    root@% vi /var/db/scripts/event/test.slax
    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";
    import "../import/junos.xsl";
    match / {
    /* The configuration change must be defined */
    var $configuration-change = <configuration> {
                    < protocol> {
                      <ospf> {
                         <area> {
                           <name> "0.0.0.0";
                           <interface> {
                             <name> "ge-0/0/0.0";
                             <metric> "10";
                            }
                        }
                    }
             }
    }
    var $connection = jcs:open();
    var $results := { call jcs:load-configuration( $connection, $configuration =
    $configuration-change ); }
    <event-script-results>{
      if( $results//xnm:error ) {
      for-each( $results//xnm:error ) {
        <output> message;
      }
    }
    }
    var $close-results = jcs:close($connection);
    }

     

    Still not working

     

     

    192.168.1.0/24     Intra Network    IP          100 ge-0/0/0.0
      area 0.0.0.0, origin 10.0.2.26, priority low
    

     at the moment don't want to get any output, just metric update to routing peers.

     

    thanks



  • 4.  RE: Help with script to change route metric in ospf

     
    Posted 01-11-2016 02:14

    Here is the op script that I was using.

     

    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";
    
    import "../import/junos.xsl";
    
    
    match / {
      <op-script-results> {
        var $configuration-change = <configuration>{
          < protocols> {
            <ospf> {
              <area> {
                <name> "0.0.0.0";
                <interface> {
                  <name> "ge-0/0/0";
                  <metric> "10";
                }
              }
            }
          }
        }
    
        var $connection = jcs:open();
        var $results := { call jcs:load-configuration( $connection, $configuration = $configuration-change ); }
        if( $results//xnm:error ) {
          for-each( $results//xnm:error ) {
            <output> message;
          }
        }
        var $close-results = jcs:close($connection);
      }
    }
    

    Running the script updates the configuration with the new metric.

     

    > show configuration protocols ospf    
    area 0.0.0.0 {
        interface ge-0/0/0.0;
    }
    
    > op test 
    
    > show configuration protocols ospf    
    area 0.0.0.0 {
        interface ge-0/0/0.0 {
            metric 10;
        }
    }
    

    I'll spend a few minutes and convert the op script into an event script and check to see that it behaves properly, but since op scripts and event scripts are pretty much the same thing (albeit with/without an event trigger) I would expect that this will still work.

     

    Regards,

    Andy



  • 5.  RE: Help with script to change route metric in ospf
    Best Answer

     
    Posted 01-11-2016 02:51

    Here is a simple event script version:

     

    Verification that the metric isn't present.

    > show configuration protocols ospf   
    area 0.0.0.0 {
        interface ge-0/0/0.0;
    }

    The event policy configuration, based on just a single time-event for simplicity of testing.

    > show configuration event-options 
    generate-event {
        myEventTrigger time-of-day "10:30:00 +0000";
    }
    policy myEventTrigger {
        events myEventTrigger;
        then {
            event-script myEvent.slax;
        }
    }
    event-script {
        file myEvent.slax;
    }

    The event script itself:

    % cat /var/db/scripts/event/myEvent.slax 
    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";
    
    import "../import/junos.xsl";
    
    match / {
      <event-script-results> {
        var $configuration-change = <configuration>{
          < protocols> {
            <ospf> {
              <area> {
                <name> "0.0.0.0";
                <interface> {
                  <name> "ge-0/0/0";
                  <metric> "10";
                }
              }
            }
          }
        }
    
        var $connection = jcs:open();
        var $results := { call jcs:load-configuration( $connection, $configuration = $configuration-change ); }
        if( $results//xnm:error ) {
          for-each( $results//xnm:error ) {
            <output> message;
          }
        }
        var $close-results = jcs:close($connection);
      }
    }
    

    Finally, the results once the script has executed:

    > show system uptime 
    Current time: 2016-01-11 10:24:53 UTC
    
    > show configuration protocols ospf    
    area 0.0.0.0 {
        interface ge-0/0/0.0;
    }

    show system uptime                   
    Current time: 2016-01-11 10:31:10 UTC
    > show configuration protocols ospf    
    area 0.0.0.0 {
        interface ge-0/0/0.0 {
            metric 10;
        }
    }
    
    > show log escript.log 
    .
    .
    .
    Jan 11 10:30:01 event script processing begins
    Jan 11 10:30:01 reading event details
    Jan 11 10:30:01 testing event details
    Jan 11 10:30:01 running event script 'myEvent.slax'
    Jan 11 10:30:01 opening event script '/var/db/scripts/event/myEvent.slax'
    Jan 11 10:30:01 reading event script 'myEvent.slax'
    Jan 11 10:30:56 event script output
    Jan 11 10:30:56 begin dump
    <?xml version="1.0"?>
    <event-script-results xmlns:junos="http://xml.juniper.net/junos/*/junos" xmlns:xnm="http://xml.juniper.net/xnm/1.1/xnm" xmlns:jcs="http://xml.juniper.net/junos/commit-scripts/1.0"/>
    Jan 11 10:30:56 end dump
    Jan 11 10:30:56 inspecting event output 'myEvent.slax'
    Jan 11 10:30:56 finished event script 'myEvent.slax'
    Jan 11 10:30:56 event script processing ends
    

     Regards,

    Andy



  • 6.  RE: Help with script to change route metric in ospf

    Posted 01-12-2016 21:41

     

     

     

    Hey first of all, thank you for this much input. I have changed my configuration to reflect your inputs

     

    Currently I've this configured.

     

    ospf {
        area 0.0.0.0 {
            interface ge-0/0/0.0;
        }
    }
    

     

    this is the modified script. it is getting trigger if rpm probe fails.

     

     

     

    root@% cat /var/db/scripts/event/test.slax
    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";
    import "../import/junos.xsl";
    match / {
    /* The configuration change must be defined */
    <event-script-results>{
    var $configuration-change = <configuration> {
                    < protocols> {
                      <ospf> {
                         <area> {
                           <name> "0.0.0.0";
                           <interface> {
                             <name> "ge-0/0/0.0";
                             <metric> "10";
                            }
                        }
                    }
             }
    }
    var $connection = jcs:open();
    var $results := { call jcs:load-configuration( $connection, $configuration =
    $configuration-change ); }
      if( $results//xnm:error ) {
      for-each( $results//xnm:error ) {
        <output> message;
      }
    }
    var $close-results = jcs:close($connection);
    }
    }
    

     

     

     

     

     

     

    an  7 09:41:16 Executing command </usr/libexec/ui/cscript -mescriptdefinition -f  test.slax -M >
    Jan  7 09:41:16 CHLD process /usr/libexec/ui/cscript with pid <13147> terminated successfully
    Jan  7 09:41:16 Executing command </usr/sbin/mgd -P/tmp/eventd-temp-db.Qc6aY xml-load /tmp/escript-config-xml.HT89n >
    Jan  7 09:41:17 CHLD process /usr/sbin/mgd with pid <13148> terminated successfully
    

     

     

    But its not changing the configuration

     

    ospf {
        area 0.0.0.0 {
            interface ge-0/0/0.0;
        }
    }

    Now I don't know may be its vSRX problem or something else.

     

    can you tell me how did you enable that trace of script debugging.

     

     



  • 7.  RE: Help with script to change route metric in ospf

    Posted 01-12-2016 21:59

    Hey Man. you did the trick. I have couple of typos in my script. notebly case sensitive junos not Junos 😄

     

    Well Its working now.

     

     

    Now i need a script to revert back to original in the event of probe success. but here is the problem . probe sucess will happen like 99% of time. and if i have script executing 99% of time writing the same config again n again, well, now. that's the problem i need to address .



  • 8.  RE: Help with script to change route metric in ospf

     
    Posted 01-13-2016 02:30

    @vMicroMe wrote:

    Now i need a script to revert back to original in the event of probe success. but here is the problem . probe sucess will happen like 99% of time. and if i have script executing 99% of time writing the same config again n again, well, now. that's the problem i need to address .


    OK, this can be handled by the script.  You can grab the configuration from the device within the event script and perform a check to see if a change is necessary or not, and only apply the change if it is necessary. 

     

    If you take a look at the following script then hopefully it will be apparent how the committed database can be loaded (or just a subset in this case), and then query those results to see if the metric is configured or not, and only apply the configuration when necessary.   Of course this is just an example and most likely doesnt follow the exact logic that you will need, but it should be reasonably clear of the sort of approach required.

     

    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";
    
    import "../import/junos.xsl";
    
    match / {
      <op-script-results> {
        var $connection = jcs:open();  /* create a connection to reuse */
    
        /* rpc to get some configuration */
        var $rpc-get-configuration = <get-configuration database="committed" inherit="inherit"> {
          <configuration> {
            <protocols> {
              <ospf> {
                <area> {
                  <name> "0.0.0.0";
                  <interface> {
                    <name> "ge-0/0/0";
                  }
                }
              }
            }
          }
        }
        var $get-configuration = jcs:execute($connection, $rpc-get-configuration);
    
        if ($get-configuration/protocols/ospf/area[name="0.0.0.0"]/interface[name="ge-0/0/0.0"]/metric == 10) {
          /* nothing to do */
        } else {
          var $configuration-change = <configuration>{
            <protocols> {
              <ospf> {
                <area> {
                  <name> "0.0.0.0";
                  <interface> {
                    <name> "ge-0/0/0";
                    <metric> "10";
                  }
                }
              }
            }
          }
    
          var $results := { call jcs:load-configuration( $connection, $configuration = $configuration-change ); }
          if( $results//xnm:error ) {
            for-each( $results//xnm:error ) {
              <output> message;
            }
          }
        }
        expr jcs:close($connection);  /* close the connection */
      }
    }

     

    Regards,

    Andy