Junos Automation (Scripting)
Reply
Recognized Expert
aweck
Posts: 255
Registered: ‎07-24-2009
0

Writing to files from an op script

Does anyone have experience writing to files from an op script?  I've tried using jcs:execute functions with rpc commands (as shown below) without success.  The script completes successfully and even outputs what I would expect in the results-* variables, but when I try to view the files, they are not there.

 

            var $connection = jcs:smileysurprised:pen();
            var $rpc-rsi = <rpc> {
                        <command> {
                            expr "request support information | save /var/tmp/rsi.txt"; } }
            var $results-rsi = jcs:execute($connection, $rpc-rsi);
            var $rpc-archive = <rpc> {
                            <command> {
                                expr "file archive source /var/tmp/rsi.txt compress destination /var/tmp/rsi.tgz"; } }
            var $results-archive = jcs:execute($connection, $rpc-archive);

Juniper Elite Partner
JNCIE-ENT #63, JNCIE-SP #705, JNCIE-SEC #17, JNCIS-FWV, JNCIS-SSL
Super Contributor
jschulman
Posts: 144
Registered: ‎11-03-2009
0

Re: Writing to files from an op script

You can use the <exsl:document> or <redirect:write> extensions to write to a local file.

 

You can find information on <exsl:document> here: http://www.exslt.org/exsl/elements/document/index.html

and

You can find information on <redirect:write> here:http://xml.apache.org/xalan-j/extensionslib.html

 

Hope this helps!

-- Jeremy

Cheers,
-- Jeremy

@nwkautomaniac
Recognized Expert
aweck
Posts: 255
Registered: ‎07-24-2009
0

Re: Writing to files from an op script

Thanks for the info.  I was able to direct the output from a command into a file using the file-put command:

 

            var $rpc-rsi = <command> { expr "request support information"; }
            var $rsi-output = jcs:execute($connection, $rpc-rsi);
                var $rpc-rsi-write = {
                        <file-put> {
                      <filename> "/var/tmp/baseline-info/rsi.txt";
                        <encoding> "ascii";
                        <delete-if-exist>;
                        <file-contents> $rsi-output;
                    }
                }

However, I am having problems trying to compress this file (and others) before copying to an FTP server.  Coping to an FTP server is pretty straightforward using the file-copy command, but there does not seem to be an option to compress using the file-copy command and I don't see any archive type command.  I tried configuring the following:

 

            var $rpc-archive = <command> { expr "file archive source /var/tmp/baseline-info compress destination ftp://<user>:<pass>@<ip>/<dir>/baseline-info.tgz"; }
            var $archive-call = jcs:execute($connection, $rpc-archive);

 

But when I enabled traceoptions on the op script I saw that this operation is not allowed:

 

<rpc-reply xmlns:junos="http://xml.juniper.net/junos/10.1R3/junos" xmlns="">
<output>
/usr/bin/tar: Removing leading `/' from member names
</output>
<xnm:error xmlns="http://xml.juniper.net/xnm/1.1/xnm" xmlns:xnm="http://xml.juniper.net/xnm/1.1/xnm">
<message>
Operation allowed only from CLI
</message>
</xnm:error>
</rpc-reply>

 

Does anyone know of an archive type command for an op script or are there plans to support a 'file-archive' operational command?

Juniper Elite Partner
JNCIE-ENT #63, JNCIE-SP #705, JNCIE-SEC #17, JNCIS-FWV, JNCIS-SSL
Copyright© 1999-2013 Juniper Networks, Inc. All rights reserved.