Automation

last person joined: yesterday 

Ask questions and share experiences about Apstra, Paragon, and all things network automation.
  • 1.  [Junoscript] script to shutdown or restart interface

    Posted 05-03-2013 02:11

    Hi,

     

    I need your advice to understand what's going wrong in my script, running on SRX3400

    I change a litle bit this script: toggle-interface.slax to have the possibility to desactivate the Interface (xe-3/0/0) and not the unit of this interface. This script is trigger by a shutdown detection of another interface and it works well.

     

    BUT, I use this same script to restart the interface and it failed without any reason. I try to restart the interface, not the unit (which is still up), but it doesn't work.

     

    My trigger to restart seems to be good because at the beginning, I'm playing with shutdown/restart the UNIT of the interface and it worked.

     

    So I think the mistake is easy to find but I need your help.

    Thank you.
    Best Regards.

     

    ------------- here is my srx config to start the script --------------------

    show configuration event-options

    policy shutdown-intxe300-ifxe301-is-down {

        events snmp_trap_link_down;

        attributes-match {

            snmp_trap_link_down.interface-name matches xe-3/0/1;

        }

        then {

            event-script toggle-interface.slax {

                arguments {

                    interface xe-3/0/0;

                    new_intf_state disable;

                    silent 1;

                }

            }

        }

    }

    policy start-intxe300-ifxe301-is-up {

        events snmp_trap_link_up;

        attributes-match {

            snmp_trap_link_up.interface-name matches xe-3/0/1;

        }

        then {

            event-script toggle-interface.slax {

                arguments {

                    interface xe-3/0/0;

                    new_intf_state enable;

                    silent 1;

                }

            }

        }

    }

    event-script {

        file disable-interface.slax;

    }

    --------------------------------------------------------------------------------------

     

    ------------- here is my new toggle-interface.slax script --------------

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

    /*
    * Version: 1.0
    * Author: Juniper Networks
    * v2.0 -20130430-> Action sur l'interface même et non sur une sous unité "unit"
    */

    /*
    * Script toggles interface up or down based on passed args.
    * It's designed to work with RPM scripts and be called as part
    * of a JUNOS event on RPM failure or sucess. No state is
    * kept between RPM probes on interface state, so script
    * needs to read config for each time a probe passes or failes
    * to check current interface state and new one passed in arg list
    *
    */

    var $arguments = {
    <argument> {
    <name> "silent";
    <description> "Decides where the output will go, 0 -> stdout, 1 -> syslog, 2 -> none";
    }
    <argument> {
    <name> "interface";
    <description> "Interface to deactivate";
    }
    <argument> {
    <name> "new_intf_state";
    <description> "New state of the interface disable -> disable interface, 1 -> enable interface";
    }

    }


    match / {

    /*
    * Parse interface to get IFD and IFL
    */
    var $split = jcs:split("\\.", $interface);
    var $ifd = $split[1];
    var $unit = $split[2];

    var $rpc-config-req = <get-configuration database="committed" inherit="inherit">;
    var $configuration = jcs:invoke($rpc-config-req);
    if ($new_intf_state == "enable") {
    /*
    * If interface is already disabled and new interface state is enable,
    * populate the $xml var below to enable interface. SOMETHING SHOULD BE WRONG HERE
    */
    if ($configuration/interfaces/interface[name=$ifd]/unit[name=$unit]/disable) {
    var $xml = {
    <configuration> {
    <interfaces> {
    <interface> {
    <name> $ifd;
    <disable delete="disable">;
    } /* end <interface> */
    } /* end <interfaces> */
    } /* end <configuration> */
    } /* end var $xml */

    call doConfigChange($xml);
    }
    } else if ($new_intf_state == "disable") {
    /* 
    * If interface is enabled and new interface state is disable,
    * populate the $xml var below to disable interface
    */
    if (not($configuration/interfaces/interface[name=$ifd]/unit[name=$unit]/disable)) {
    var $xml = {
    <configuration> {
    <interfaces> {
    <interface> {
    <name> $ifd;
    <disable>;
    } /* end <interface> */
    } /* end <interfaces> */
    } /* end <configuration> */
    } /* end var $xml */

    call doConfigChange($xml);

    } else {
    /*
    * unrecognised new interface state.
    */
    call emit-error($message = "unrecognised new intf state");
    }
    }

    template doConfigChange($xml) {
    /*
    * Open connection with mgd
    */
    var $con = jcs:open();

    if (not($con)) {
    call emit-error($message = "Not able to connect to local mgd");
    }
    var $config-private = <open-configuration> {
    <private>;
    }
    var $private-results = jcs:execute($con, $config-private);

    var $load-configuration = <load-configuration> {
    copy-of $xml;
    }

    var $load-results = jcs:execute($con, $load-configuration);
    /*
    * Use load-configuration template defined in junos.xsl to load and
    * commit the configuration
    */

    var $commit-configuration = <commit-configuration>;

    var $commit-results = jcs:execute($con, $commit-configuration);

    var $close-private = <close-configuration>;

    var $close-configuration-results = jcs:execute($con, $close-private);
    var $close-results = jcs:close($con);

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

    if ($commit-results//xnm:error) {
    for-each ($results//xnm:error) {
    call emit-error($message = message);
    }
    } else {
    if ($new_intf_state == "enable") {
    call emit-success($message = "Activated interface");
    } else if ($new_intf_state == "disable") {
    call emit-success($message = "Deactivated interface");
    }
    }

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

    template emit-success($message) {

    if ($silent == 0) {
    expr jcs:output($message);
    } else if ($silent == 1) {
    expr jcs:syslog("user.info","toggle-interface.slax[Success]: ", $message);
    }
    }

    template emit-error($message) {

    if ($silent == 0) {
    expr jcs:smileysurprised:utput($message);
    } else if ($silent == 1) {
    expr jcs:syslog("user.error", "disable-interface.slax[Error]: ", $message);
    }
    }

    template emit-warn($message) {

    if ($silent == 0) {
    expr jcs:output($message);
    } else if ($silent == 1) {
    expr jcs:syslog("user.warning", "disable-interface.slax[Warning]: ", $message);
    }
    }

     

    -------------------------------------------------------------------------------------



  • 2.  RE: [Junoscript] script to shutdown or restart interface
    Best Answer

    Posted 05-03-2013 09:34

    Hello,

    Since You are passing the interface name without dots (.), then the jcs:split() won't work and Your split[] aray will not be populated. I am talking about this bit:

     

     

       var $split = jcs:split("\\.", $interface);
       var $ifd = $split[1];
       var $unit = $split[2];
    

     Please change this code to:

     

       var $split = jcs:split("\\.", $interface);
       var $ifd = $interface;
       var $unit = $split[2];
    

     HTH

    Thanks
    Alex



  • 3.  RE: [Junoscript] script to shutdown or restart interface

    Posted 05-06-2013 11:33

    HI,

     

    Thank you for your answer.
    I will try it in few days and give you an feedback.

     

    B.R.