Automation

last person joined: 3 days ago 

Ask questions and share experiences about Apstra, Paragon, and all things network automation.
  • 1.  Disable interface script fail to commit

    Posted 10-08-2013 11:55

    Hi guys, i have this error message when committing the simple interface disable script

     

    Any ideas why?

     

     

    commit

     

    error: Invalid type
    error: runtime error: file /var/db/scripts/commit/disable-interface.slax line 118 element for-each
    error: Failed to evaluate the 'select' expression.
    error: 3 errors reported by commit scripts
    error: commit script failure

     

     

     

    /*
     * Example script to demonstrate how a configuration can be committed 
     * from op-script.
     * 
     * This script by default tries to deactive interface 'ge-0/0/2'
     *
     * (i.e) Commit the following configuration
     *         "set interfaces ge-0/0/2 disable"
     *
     * Parameters: $interface -> Interface to disable
     *             $silent -> Decides where the output will go. 
     *                          0 -> (Default) Output to stdout
     *                          1 -> Output to syslog
     *
     * Same script can be triggered from event-policy configuration to deactivate
     * an interface on some specific conditions.
     *
     * For Example: 
     *
     * The following event-policy will trigger this script when
     * PING_TEST_FAILED event occurs. (PING_TEST_FAILED will be genereated by rpm 
     * when the ping test is failed)
     *
     * This policy also demonstrates, how to restrict the execution of same script 
     * again and again because of multiple occurences of the same event.
     *
     * Here, the only first occurance of PING_TEST_FAILED will trigger the script
     * and the script will be triggerd again only when the PING_TEST_FAILED did
     * not occur in the last 5 mins (300 secs)
     *
    policy disable-interface-on-ping-failure {
        events ping_test_failed;
        within 300 {
            trigger on 1; 
        }
        then {
            event-script disable-interface.slax {
                arguments {
                    silent 1;
                }
            }
        }
    }
     * 
     * Sample rpm configuration used to test this.
     *
    rpm {
        probe icmp-ping-probe {
            test ping-probe-test {
                probe-type icmp-ping;
                target address 10.1.2.1;
                test-interval 60;
            }
        }
    }
     *
     *
     */
    
    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";
    
    
    var $arguments = {
        <argument> {
    	<name> "interface";
    	<description> "Interface to deactivate";
        }
        <argument> {
    	<name> "silent";
    	<description> "Decides where the output will go, 0 -> stdout, 1 -> syslog";
        }
    }
    
    param $interface = "ge-0/0/2";
    param $silent = 0;
    
    match / {
    
        /*
         * Open connection with mgd
         */
        var $con = jcs:open();
    
        if (not($con)) {
    	call emit-error($message = "Not able to connect to local mgd");
        }
    
        /*
         * Configuration to disable the interface
         */
        var $xml = {
    	<configuration> {
    	     <interfaces> {
    		 <interface> {
    		     <name> $interface;
    		     <disable>;
    		 }
    	     }
    	}
        }
    
        /*
         * Use load-configuration template defined in junos.xsl to load and
         * commit the configuration
         */
        var $results = {
    	call jcs:load-configuration($connection = $con, $configuration = $xml);
        }
    
        /*
         * Emit warnings
         */
        for-each ($results//xnm:warning) {
    	call emit-warn($message = message);
        }
    
        if ($results//xnm:error) {
    	for-each ($results//xnm:error) {
    	    call emit-error($message = message);
    	}
        } else {
    	call emit-success($message = "Successfully deactivated the interface");
        }
    
        if (not($silent)) {
    	<op-script-results> {
    	    copy-of $results;
    	}
        }
    
        /*
         * Close the mgd connection
         */
        expr jcs:close($con);
    
    }
    
    template emit-success($message)
    {
        if ($silent) {
    	expr jcs:syslog("user.info", "disable-interface.slax[Success]: ", $message);
        } else {
    	expr jcs:output($message);
        }
    }
    
    template emit-error($message) 
    {
        if ($silent) {
    	expr jcs:syslog("user.error", "disable-interface.slax[Error]: ", $message);
        }
    }
    
    template emit-warn($message)
    {
        if ($silent) {
    	expr jcs:syslog("user.warning", "disable-interface.slax[Warning]: ", $message);
        }
    }

     

     

     

    Thanks!

     



  • 2.  RE: Disable interface script fail to commit

    Posted 10-09-2013 10:44

    Hello,

    I believe You have to use

        var $results := {
    	call jcs:load-configuration($connection = $con, $configuration = $xml);
        }

     as per http://www.juniper.net/techpubs/en_US/junos/topics/reference/scripting/junos-script-automation-template-jcs-load-configuration.html

    Specifically,

     

    The := operator ensures that the disable-results variable is a node-set rather than a result tree fragment so that the script can access the contents

     HTH

    Thanks

    Alex



  • 3.  RE: Disable interface script fail to commit

    Posted 10-10-2013 02:04

    Nop Alex, then it drops another error 😛

     

    Thanks for a try 🙂

     



  • 4.  RE: Disable interface script fail to commit

    Posted 10-10-2013 02:22

    Thats the "clear" configuration and script:

     

    set services rpm probe icmp-ping-probe test ping-probe-test probe-type icmp-ping
    set services rpm probe icmp-ping-probe test ping-probe-test target address 40.0.0.1
    set services rpm probe icmp-ping-probe test ping-probe-test test-interval 60

     

    set event-options policy disable-interface-on-ping-failure events ping_test_failed
    set event-options policy disable-interface-on-ping-failure within 3 trigger on
    set event-options policy disable-interface-on-ping-failure within 3 trigger 1
    set event-options policy disable-interface-on-ping-failure then event-script disable-interface.slax arguments silent 0

     

     

    ***

     

     

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


    var $arguments = {
    <argument> {
    <name> "interface";
    <description> "Interface to deactivate";
    }
    <argument> {
    <name> "silent";
    <description> "Decides where the output will go, 0 -> stdout, 1 -> syslog";
    }
    }

    param $interface = "ae0";
    param $silent = 0;

    match / {

    var $con = jcs: open();

    if (not($con)) {
    call emit-error($message = "Not able to connect to local mgd");
    }

    var $xml = {
    <configuration> {
    <interfaces> {
    <interface> {
    <name> $interface;
    <disable>;
    }
    }
    }
    }

    var $results = {
    call jcs:load-configuration($connection = $con, $configuration = $xml);
    }

    for-each ($results//xnm:warning) {
    call emit-warn($message = message);
    }

    if ($results//xnm:error) {
    for-each ($results//xnm:error) {
    call emit-error($message = message);
    }
    } else {
    call emit-success($message = "Successfully deactivated the interface");
    }

    if (not($silent)) {
    <op-script-results> {
    copy-of $results;
    }
    }

    expr jcs:close($con);

    }

    template emit-success($message)
    {
    if ($silent) {
    expr jcs:syslog("user.info", "disable-interface.slax[Success]: ", $message);
    } else {
    expr jcs: output($message);
    }
    }

    template emit-error($message)
    {
    if ($silent) {
    expr jcs:syslog("user.error", "disable-interface.slax[Error]: ", $message);
    }
    }

    template emit-warn($message)
    {
    if ($silent) {
    expr jcs:syslog("user.warning", "disable-interface.slax[Warning]: ", $message);
    }
    }

     

     

    If i delete next phase, script loas succesfully but not making it job (interface isnt going down)

     

    for-each ($results//xnm:warning) {
    call emit-warn($message = message);
    }

    if ($results//xnm:error) {
    for-each ($results//xnm:error) {
    call emit-error($message = message);
    }
    } else {
    call emit-success($message = "Successfully deactivated the interface");
    }

    if (not($silent)) {
    <op-script-results> {
    copy-of $results;
    }

     

    Any ideas? 😞



  • 5.  RE: Disable interface script fail to commit

    Posted 10-10-2013 02:53

    Hello,

    I took Your original script, modified just one line as per my suggestion and it works for me on SRX210 JUNOS 10.4R11.5, see below:

    aarseniev@dale> show version 
    Oct 10 11:34:51
    Hostname: dale
    Model: srx210h-p-m
    JUNOS Software Release [10.4R11.5]
    
    aarseniev@dale> op di 
    Oct 10 11:35:23
    Successfully deactivated the interface
    
    aarseniev@dale> show configuration system scripts 
    Oct 10 11:35:31
    op {
        file di.slax;
    }
    
    aarseniev@dale> file show /var/db/scripts/op/di.slax | no-more 
    Oct 10 11:35:45
    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";
     
     
    var $arguments = {
        <argument> {
            <name> "interface";
            <description> "Interface to deactivate";
        }
        <argument> {
            <name> "silent";
            <description> "Decides where the output will go, 0 -> stdout, 1 -> syslog";
        }
    }
     
    param $interface = "ge-0/0/2";
    param $silent = 0;
     
    match / {
     
        /*
         * Open connection with mgd
         */
        var $con = jcs:open();
     
        if (not($con)) {
            call emit-error($message = "Not able to connect to local mgd");
        }
     
        /*
         * Configuration to disable the interface
         */
        var $xml = {
            <configuration> {
                 <interfaces> {
                     <interface> {
                         <name> $interface;
                         <disable>;
                     }
                 }
            }
        }
     
        /*
         * Use load-configuration template defined in junos.xsl to load and
         * commit the configuration
         */
        var $results := {
            call jcs:load-configuration($connection = $con, $configuration = $xml);
        }
     
        /*
         * Emit warnings
         */
        for-each ($results//xnm:warning) {
            call emit-warn($message = message);
        }
     
        if ($results//xnm:error) {
            for-each ($results//xnm:error) {
                call emit-error($message = message);
            }
        } else {
            call emit-success($message = "Successfully deactivated the interface");
        }
     
        if (not($silent)) {
            <op-script-results> {
                copy-of $results;
            }
        }
     
        /*
         * Close the mgd connection
         */
        expr jcs:close($con);
     
    }
     
    template emit-success($message)
    {
        if ($silent) {
            expr jcs:syslog("user.info", "disable-interface.slax[Success]: ", $message);
        } else {
            expr jcs:output($message);
        }
    }
     
    template emit-error($message)
    {
        if ($silent) {
            expr jcs:syslog("user.error", "disable-interface.slax[Error]: ", $message);
        }
    }
     
    template emit-warn($message)
    {
        if ($silent) {
            expr jcs:syslog("user.warning", "disable-interface.slax[Warning]: ", $message);
        }
    }

     Please run a diff on Your original script and mine to see the changes. There should be just 1 change (= vs :=).

     


    @Sten wrote:

    Nop Alex, then it drops another error 😛

     

     


    Then it seems You introduced another change into Your original script as posted and did not tell anyone 🙂

    HTH

    Thanks

    Alex

     



  • 6.  RE: Disable interface script fail to commit

    Posted 10-10-2013 04:51

    This is what i get when i try to commit with ":="

     

     

    root@tst-mx80-dmzA# run show system users
    12:18PM up 69 days, 2:19, 1 user, load averages: 0.21, 0.21, 0.17
    USER TTY FROM LOGIN@ IDLE WHAT
    root u0 - 02Aug13 - cli

    [edit]


    root@tst-mx80-dmzA# commit


    error: configuration database locked by:
    root terminal u0 (pid 8727) on since 2013-10-10 12:17:19 UTC
    running commit script
    error: 1 error reported by commit scripts
    error: commit script failure

     

    and ofcoz:

     

    root@tst-mx80-dmzA> request system logout pid 8727
    error: You do not want to kill yourself

     

     

    with "=" i get the previous error (with f"or-each")

    and i  commit only when i use "=" and delete "for-each" part

     



  • 7.  RE: Disable interface script fail to commit

    Posted 10-10-2013 04:53

     BTW my ver is 11.4 R7



  • 8.  RE: Disable interface script fail to commit
    Best Answer

    Posted 10-10-2013 14:40

    Hello,

    I just noticed You are trying to use this script as a commit-script.

    This is wrong, please use it as op script and/or event-script.

    With my modification (:= vs 😃 of course.

    To use this script as event-script, do the following:

    1/ place the script into /var/db/scripts/event/ directory

    2/ add following lines into MX80 config:

     

    set event-options event-script file <filename.slax>

     To use this script as op script, do the following:

    1/ place the script into /var/db/scripts/op directory

    2/ add following lines into MX80 config:

     

    set system scripts op file <filename.slax>

     

    And please have a good read into this Day1 book

    https://www.juniper.net/us/en/community/junos/training-certification/day-one/automation-series/applying-junos-automation/

    - in order to understand the difference between op, commit and event scripts.

    HTH

    Thanks

    Alex



  • 9.  RE: Disable interface script fail to commit

    Posted 10-14-2013 01:30

    Dude 😄  U rox! 
    working as event-script (bah so obvious mistake!)



  • 10.  RE: Disable interface script fail to commit

    Posted 07-29-2016 11:41

    Hi,

       I am trying to run an event script.

    I have saved the script in /var/db/dcripts/event directory and have configured the below confg in my vmx 

    generate-event {
    test1 time-interval 60;
    }
    policy test2 {
    events test1;
    then {
    event-script test.slax;
    }
    }
    event-script {
    file test.slax;
    }

    [edit event-options]
    root# commit
    error: invalid filename: /var/db/scripts/event//test.slax
    error: Reading the configuration from event scripts failed
    error: configuration check-out failed

     

    this is my test.slax file:

     

    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 ext = "http://xmlsoft.org/XSLT/namespace";

    match / {
         <op-script-results>{
              var $cmd = <command> "request pfe execute command "show pfe statistics traffic" target fpc0 ";
              var $out = jcs:invoke($cmd);
              var $lines = jcs.break_lines($out);

              for-each ($lines) {
                   if (contains(., "Policer Drop Counts")) {
                        var $drops = "([0-9]+)";
                        if ($drops > 0)
                             var $syslog = " Alert!!!! Bandwidth Exceeded!"
                        var $rpc = <request-snmp-utility-mib-set> {
                             <object-type> "string";
                             <instance> "bandwidth limit";
                             <object-value> $drops;
                            }
                         var $res = jcs:invoke($rpc);
                      }
                }
            }
    }

    Can anyone help me on where I am going wrong?

    Thank You