Junos Automation (Scripting)
Posts: 8
Registered: ‎03-23-2012
Accepted Solution

op script to desactivate unused interfaces



i'm trying to code an op script to desactivate unused interfaces for MX platform

it works good, but the problem is that my script commit changes one by one (interface by interface), so for some cases, i loose all may rollback files.

for each unconfigured interface, changes are made and a commit also, so is there a solution to have just one commit at the end of the script.



this is my scipt :


 * This script disables all unconfigured ge interfaces.
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> {
    /* Configuration du groupe PortNotUsed*/
   var $config-changes = {
            <configuration> {
                <groups> {
                    <name> "PortNotUsed";
                    <interfaces> {
                        <interface> {
                            <name> "<*>";
                            <description> "**** Not Used ****" ;
    var $load-action = "merge";
    var $conn_handle = jcsSmiley Surprisedpen();
    var $results := {
    call jcs:load-configuration( $action=$load-action, $configuration=$config-changes, $connection=$conn_handle);
    var $close-results = jcs:close($conn_handle);
    var $interfaces = jcs:invoke( "get-interface-information" );
    var $ge-interfaces = $interfaces/physical-interface[starts-with(name, "ge-") or starts-with(name, "xe-")];
    var $interface-hierarchy = interfaces;
    for-each( $ge-interfaces ) {
        var $commande1 = "show configuration interfaces "_ name;
        var $rpc1 = <command> $commande1;
        var $rpcbis1 = jcs:invoke ($rpc1) ;
        if (jcs:empty($rpcbis1)) {
            <output> "Disabling unconfigured interface: " _ name;
            var $sys = "Desactivation du port "_name;
            expr jcs:syslog(12, $sys );
            var $config-changes1 = {
                <configuration> {
                    <interfaces> {
                        <interface> {
                            <name> name;
                            <apply-groups> "PortNotUsed";
            var $load-action1 = "merge";
            var $conn_handle1 = jcsSmiley Surprisedpen();
            var $results1 := {
                call jcs:load-configuration( $action=$load-action1, $configuration=$config-changes1, $connection=$conn_handle1);
            var $close-results1 = jcs:close($conn_handle);
            else {
        <xnm:warning> {
        <message> "is already configured:" _ name;


thank you very much for your help.

Recognized Expert
Posts: 198
Registered: ‎03-17-2010

Re: op script to desactivate unused interfaces

[ Edited ]

Hi, in order to commit only at the end of the script, you should use the jcs:execute function instead of the jcs:load-configuration.

I'd also suggest you to lock the configuration at the beginning of the script.

So these are the steps to follow:


1. Open the connection and lock the configuration database, before entering the for-each cycle:


var $connection = jcs:open();
var $config-lock =   <lock-configuration>;
var $lock-results = jcs:execute( $connection, $config-lock );
			if ($lock-results//xnm:error) { <xnm:error>{<message> "Configuration locking failed!";}}

2. within the for-each cycle, load each configuration change:


var $load-configuration =   <load-configuration> {
                                            copy-of $config-changes;
var $load-results = jcs:execute( $connection, $load-configuration );
			if ($load-results//xnm:error) { <xnm:error>{<message> "Configuration loading failed!";}}

 3. After the for-each cycle, commit the configuration, unlock the database and close the connection:


var $commit-configuration = <commit-configuration>;
var $commit-results = jcs:execute( $connection, $commit-configuration );
var $config-unlock =   <unlock-configuration>;
var $unlock-results = jcs:execute( $connection, $config-unlock );
            if ($unlock-results//xnm:error) { <xnm:warning>{<message> "Configuration unlocking failed!";}}
var $close-results = jcs:close( $connection );
if ($close-results//xnm:error) { <xnm:warning>{<message> "Disconnection from the router failed!";}}

 You can find some reference about those functions in the Day One book "This Week: Junos Automation Reference for SLAX 1.0".


I hope it helps!







(If this post helped you, please mark it as an "Accepted Solution"; kudos are also appreciated!)

Posts: 8
Registered: ‎03-23-2012

Re: op script to desactivate unused interfaces

Thank you very much.


Posts: 138
Registered: ‎09-21-2010

Re: op script to desactivate unused interfaces


Can you please share with us the final scripts to do that and is it working also for EX switches interface ??


BR ,,,
Copyright© 1999-2015 Juniper Networks, Inc. All rights reserved.