Junos Automation (Scripting)
Reply
Visitor
zhiang
Posts: 8
Registered: ‎01-10-2012
0
Accepted Solution

how to load configuration automatically through script?

Hi Experts,

  I backup the configuration file in /var/tmp directory and the name is juniper.conf.gz. I want to load and commit the "routing-option" and "interfaces" hierarchy in this file when the script be executed. Could someone help to give a few hints about how to achieve this?

 

Thanks!!

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

Re: how to load configuration automatically through script?

[ Edited ]

Hi zhiang, I wrote a script that retrieves the interfaces and routing-options configuration from an XML configuration file, and changes the configuration, loading the content of the file. Probably you can do the same with the plain text configuration file, but I found it simpler to go with XML.

In any case, backing up the configuration in XML should not be a problem (you can follow the method descripted in here, adding the "| display xml" to the show command).

So here it is the script code:

 

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> { /* Load the configuration file into script */ var $read_configuration = document("/var/tmp/XML_CONFIG.xml"); /* Retrieve interfaces hierarchy */ var $interfaces = $read_configuration//interfaces; /* Retrieve routing-options hierarchy */ var $routing-options = $read_configuration//routing-options; /* Create configuration change variable */ var $configuration = <configuration> { copy-of $interfaces; copy-of $routing-options; } /* Open the connection */ var $connection = jcs:open(); expr jcs:output( "Junoscript commit" ); var $results := { call jcs:load-configuration( $connection, $configuration ); } /* Check for errors – report them if they occurred */ if( $results//xnm:error ) { for-each( $results//xnm:error ) { <output> message; } } else { expr jcs:output( "Junoscript commit complete" ); } /* The connection is closed */ var $close-results = jcs:close($connection); } }

The load behavior is the default one (a merge), but you can use  load merge, load replace, load override, or load update as well, as described in the book Junos Automation Reference for Slax 1.0.

 

Let me know if it works for you!

 

Cheers,

Mattia

 

.................................................................................
JNCIP-ENT, JNCIP-SEC, JNCIS-SP
(If this post helped you, please mark it as an "Accepted Solution"; kudos are also appreciated!)


Visitor
zhiang
Posts: 8
Registered: ‎01-10-2012
0

Re: how to load configuration automatically through script?

Thank you Mattia, I truly appreciate your great help.

 

Zhiang

Visitor
zhiang
Posts: 8
Registered: ‎01-10-2012

Re: how to load configuration automatically through script?

Hi Mattia,

 I'm running your scirpt with a configuration file, this configuration is uploaded by event-option and the top of the file has a few different lines with "show configuration | display xml | save XML_CONFIG" :

 

<?xml version="1.0" encoding="us-ascii"?>
<junoscript xmlns="http://xml.juniper.net/xnm/1.1/xnm" xmlns:junos="http://xml.juniper.net/junos/10.4S5/junos" schemaLocation="http://xml.juniper.net/junos/10.4S5/junos junos/10.4S5/junos.xsd" os="JUNOS" release="10.4S5" hostname="" version="1.0">
<!-- session start at 2012-05-10 23:05:41 CST -->
<!-- No zombies were killed during the creation of this user interface -->
<!-- user root, class super-user -->
<rpc-reply xmlns:junos="http://xml.juniper.net/junos/10.4S5/junos">
<pipe>
<display-xml>
</display-xml>
</pipe>
<configuration junos:commit-seconds="1336660814" junos:commit-localtime="2012-05-10 22:40:14 CST" junos:commit-user="lab">

The script result is:

lab> op commit

/var/tmp/xml.xml:failed to load external entity "/var/tmp/xml.xml"
Junoscript commit
syntax error, expecting <configuration> or <configuration-text>

 

The script can work after I remove the line:

<junoscript xmlns="http://xml.juniper.net/xnm/1.1/xnm" xmlns:junos="http://xml.juniper.net/junos/10.4S5/junos" schemaLocation="http://xml.juniper.net/junos/10.4S5/junos junos/10.4S5/junos.xsd" os="JUNOS" release="10.4S5" hostname="" version="1.0">

 

and the end line:

</junoscript>

I modify the scirpt like below but it doesn't work:

        var $configuration =  <junoscript> {
   <configuration> {               
                 copy-of $interfaces;
                 copy-of $routing-options; 
         }
 }

 

 

lab> op commit
Junoscript commit
xnm:rpc results:14:smileysad:4) Opening and ending tag mismatch: load-configuration-results line 4 and junoscript
xnm:rpc results:15:smileysad:3) Premature end of data in tag rpc-reply line 3
xnm:rpc results:15:smileysad:2) Premature end of data in tag junoscript line 2
error: commit script: xml-mode: could not read content
error: invalid reply to rpc
error: could not get reply
error: xmlXPathCompiledEval: evaluation failed
error: runtime error: file /var/db/scripts/import/junos.xsl line 314 element variable
error: Failed to evaluate the expression of variable 'load-result'.

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

Re: how to load configuration automatically through script?

Hi, it looks like the issue is related to the attribute xmlns="http://xml.juniper.net/xnm/1.1/xnm"; I deleted it manually and the script worked... that's weird, since the other attributes can be handled fine!
I am thinking to a work around though, I'll let you know.
.................................................................................
JNCIP-ENT, JNCIP-SEC, JNCIS-SP
(If this post helped you, please mark it as an "Accepted Solution"; kudos are also appreciated!)


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

Re: how to load configuration automatically through script?

[ Edited ]

Hi, I found a workaround! :-)

In order to have a "clean" xml file containing the configuration, you can use an event-script which retrieves the config and writes it into a file.

So you will just have to change the then part of the event-policy which performs the backup of the configuration, in order to trigger the event-script instead of executing a command. The event-options configuration will become as follows:

 

root# show event-options 
generate-event {
    BACKUP_TRIGGER_EVENT time-interval 3600;
}
policy backup_xml_configuration {
    events BACKUP_TRIGGER_EVENT;
    then {
        event-script backup-xml-config.slax;
    }
}
event-script {
    file backup-xml-config.slax;
}

 The event-script code is quite simple:

 

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 exsl extension = "http://exslt.org/common";

import "../import/junos.xsl";
match / {
    <op-script-results> {
        /* String example */
        var $get-conf = <get-configuration format="xml">;
        var $configuration = jcs:invoke( $get-conf );
        
<exsl:document href="/var/tmp/XML_CONFIG.xml" method="xml" indent="yes"> {
    copy-of $configuration;
}
    }
}

The op-script will not need to be changed.

Let me know if it works now!

 

 

 

.................................................................................
JNCIP-ENT, JNCIP-SEC, JNCIS-SP
(If this post helped you, please mark it as an "Accepted Solution"; kudos are also appreciated!)


Visitor
zhiang
Posts: 8
Registered: ‎01-10-2012

Re: how to load configuration automatically through script?

 

Spoiler
 

Great! Thank you very much. I'm really appreciate your help. The script works good and I change it to transfer a ftp site:

 

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 exsl extension = "http://exslt.org/common";

import "../import/junos.xsl";
match / {
    <op-script-results> {
        /* String example */
        var $get-conf = <get-configuration format="xml">;
        var $configuration = jcs:invoke( $get-conf );
       
<exsl:document href="/var/tmp/XML_CONFIG.xml" method="xml" indent="yes"> {
    copy-of $configuration;
}
var $file-copy-rpc=<file-copy>{
 <source>"/var/tmp/XML_CONFIG.xml";
 <destination>"ftp://lab:lab123@172.27.103.72";
 }
var $results=jcs:invoke($file-copy-rpc);
    }
}


 

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