Support Support Downloads Knowledge Base Case Manager My Juniper Community

Knowledge Base

Search our Knowledge Base sites to find answers to your questions.

Ask All Knowledge Base Sites All Knowledge Base Sites JunosE Defect (KA)Knowledge BaseSecurity AdvisoriesTechnical BulletinsTechnotes Sign in to display secure content and recently viewed articles

How to load a configuration file obtained from NSM in XML format to the CLI in JUNOS?

0

0

Article ID: KB21311 KB Last Updated: 24 Feb 2020Version: 2.0
Summary:
Using an OP script, it is possible to load an XML formatted configuration from the command line (CLI) mode in JUNOS.
This script could be used for loading XML format configuration that is saved from NSM.
Symptoms:
The information below explains how to load XML configuration in CLI mode on JUNOS using an OP script.
  • XML format sample
lab# show | no-more | save /config/db/scripts/op/test.xml | display xml
Wrote 191 lines of output to '/config/db/scripts/op/test.xml'

root@:RE:0% cd /config/db/scripts/op
root@:RE:0% more test.xml
<configuration junos:changed-seconds="1299828253" junos:changed-localtime="2011-03-11 07:24:13 UTC">
       <version>10.3R1.9</version>
       <system>
         <root-authentication>
             <encrypted-password>$ABC123</encrypted-password>
         </root-authentication>
         <login>
            <user>
               <name>lab</name>
               <uid>2000</uid>
               <class>super-user</class>
               <authentication>
                   <encrypted-password>$ABC123</encrypted-password>
               </authentication>
            </user>
          </login>
          <services>
            <ftp>
            </ftp>
            <ssh>
            </ssh>
            <telnet>
            </telnet>
            <netconf>
                <ssh>
                </ssh>
            </netconf>
          </services>
        </system>

------------------------------omission---------------------------

</configuration>
Solution:
  1.  Create an OP script on the device.
In shell mode on JUNOS, create an OP script using the vi editor, or upload the OP script file at "/config/db/scripts/op"

lab> start shell
% su -
Password:
root@:RE:0% cd /config/db/scripts/op/
root@:RE:0% vi load-configration.slax

 

Copy and paste blow context in vi editor


*OP script file i,e, load-configration.slax

/*
* Loads the text configuration stored in <file> using the <action> specified.
*
* file = full pathname for file
* action = [merge], override, replace, update
* format = [ascii] or xml
*/
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";

/* Command-line arguments */
var $arguments = {
    <argument> {
        <name> "filename";
        <description> "The configuration file to load, including the full path.";
    }
    <argument> {
        <name> "action";
        <description> "Load action: merge, override, replace, update";
    }
    <argument> {
        <name> "format";
        <description> "Either ascii or xml format";
    }
}
param $filename;
param $action = "merge";
param $format = "ascii";

match / {
    <op-script-results> {

        if( string-length( $filename ) == 0 ) {
            <xsl:message terminate="yes"> "Configuration file must be specified.";
        }

        /* Lock the configuration */
        var $connection = jcs:open();
        var $lock-results = jcs:execute( $connection, "lock-configuration" );
        if( jcs:empty( $lock-results/..//xnm:error ) ) {

            var $load-rpc = <load-configuration url=$filename action=$action format=$format>;
            var $load-results = jcs:execute( $connection, $load-rpc );

            if( jcs:empty( $load-results/..//xnm:error ) ) {

                var $commit-results = jcs:execute( $connection, "commit-configuration" );
                copy-of $commit-results;
                var $unlock-results = jcs:execute( $connection, "unlock-configuration" );
                copy-of $unlock-results;
            }
            else {
                copy-of $load-results;

                /* Don't leave it locked because the load failed */
                var $unlock-results = jcs:execute( $connection, "unlock-configuration" );
                copy-of $unlock-results;
            }
        }
        else {
            copy-of $lock-results;
        }
        expr jcs:close( $connection );
    }
}



It is possible to upload the script file to the device via FTP. The script should be located in "/config/db/scripts/op".

root@EX4200:RE:0% cd /config/db/scripts/op
root@EX4200:RE:0% ls
test.xml stats-temp
load-configuration.slax temp.slax
 
  1. JUNOS configuration for OP script.
lab@EX4200# show system
root-authentication {
    encrypted-password "$ABC123"; ## SECRET-DATA
}
scripts {
    op {
        file load-configuration.slax;
    }
}
login {
    user lab {
        uid 2001;
        class super-user;
        authentication {
            encrypted-password "$ABC123"; ## SECRET-DATA
        }
    }
}

 
  1. Upload XML format configration to device via FTP.
  2. Run OP script
lab@EX4200> op load-configuration ?
Possible completions:
   <[Enter]>          Execute this command
   <name>             Argument name
   action             Load action: merge, override, replace, update
   detail             Display detailed output
   filename           The configuration file to load, including the full path.
   format             Either ascii or xml format
    |          Pipe through a command
{master:0}
lab@EX4200>op load-configuration filename /config/db/scripts/op/test.xml format xml action merge
configuration check succeeds commit complete

 
Comment on this article > Affected Products Browse the Knowledge Base for more articles related to these product categories. Select a category to begin.

Getting Up and Running with Junos

Getting Up and Running with Junos Security Alerts and Vulnerabilities Product Alerts and Software Release Notices Problem Report (PR) Search Tool EOL Notices and Bulletins JTAC User Guide Customer Care User Guide Pathfinder SRX High Availability Configurator SRX VPN Configurator Training Courses and Videos End User Licence Agreement Global Search