Junos
Highlighted
Junos

help with op script that save configuration file with hostname and data time

‎03-02-2020 05:21 AM

Hi all,

if it's possible I need help with a op script that save configuration file named "$hostname_$date_juniper.conf.gz" in a local destination path or on a backup-server.

My example scripts is:

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 ext = "http://xmlsoft.org/XSLT/namespace";
import "../import/junos.xsl";


var $local = jcs:open();
match / {
<op-script-results> {
var $data = {
<request-shell-execute> {
<command> "date +%F-%H:%M";
}
}
var $nome = {
<request-shell-execute> {
<command> "hostname";
}
}
var $result = jcs:execute($local,$data);
var $result2 = jcs:execute($local,$nome);
copy-of $result;
copy-of $result2;

expr jcs:close($local);
}
}
<execute-commands> {
<commands> {
<name> "file copy /config/juniper.conf.gz /tmp/" _ $result _$result2 _ "juniper.conf.gz";
}
<output-format> "text";
}

 

but it doesn't work.

 

Thanks in Advance.

 

Simone

 

 

6 REPLIES 6
Highlighted
Junos

Re: help with op script that save configuration file with hostname and data time

‎03-02-2020 09:13 PM

Hi 

 

You can try below 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";

 


match / {
<op-script-results> {

var $rpc = <get-software-information>;
var $result = jcs:invoke($rpc);
var $filename = "/config/juniper.conf.gz";
var $con = jcs:open();
var $ftpurl = "/var/tmp/juni.gz";



var $data = {
<request-shell-execute> {
<command> "date +%F-%H:%M";
}
}

 

var $name = {
<request-shell-execute> {
<command> "hostname";
}
}

 

var $result1 = jcs:execute($con,$data);
var $result2 = jcs:execute($con,$name);

copy-of $result1;
copy-of $result2;

var $fileput = {
<file-put> {
<filename>$filename;
<encoding>"ascii";
<permission>'777';
<delete-if-exist>;
<file-contents>$result;
}
}






var $out = jcs:execute($con, $fileput);

expr jcs:close($con);




var $local-out = jcs:invoke($fileput);
<output> "Saving file on local host\n" _ $local-out;

var $file-copy-rpc=<file-copy>{
<source>"/config/juniper.conf.gz";
<destination>"/var/tmp/"_
"juniper.gz"_
$result1 _
$result2;
}
var $results=jcs:invoke($file-copy-rpc);
}
}

 

Hope this helps

Highlighted
Junos

Re: help with op script that save configuration file with hostname and data time

‎03-03-2020 01:18 AM

Hi Raviky,

thanks for your help, this is the result:

 

root@router> op test
2020-03-03-09:24
router
Saving file on local host


Failed to create temporary file /config/juniper.conf.gz.e2jco: Permission denied

 

 

besides the writing problem, which I think is easy to solve, there remains the big problem of exporting the device name and date variables.

BR

Simone

 

Highlighted
Junos

Re: help with op script that save configuration file with hostname and data time

‎03-03-2020 01:44 AM

Try to run the script as root user. It should work.

Highlighted
Junos

Re: help with op script that save configuration file with hostname and data time

‎03-03-2020 02:35 AM

thanks , now it works!

But the fiel has a strange name with ?, it's possible to do something for this.... 😞

 

root@router:/var/db/scripts/op # ls -la /var/tmp
total 4200424
-rwxr-xr-x 1 root wheel 5850 Mar 3 11:30               ?2020-03-03-11-30??router?juniper.gz

Highlighted
Junos
Solution
Accepted by topic author simonev
‎03-03-2020 02:53 AM

Re: help with op script that save configuration file with hostname and data time

‎03-03-2020 02:48 AM

You can try to play around with the below snippet of the code.  It is a format problem i believe.

 

<destination>"/var/tmp/"_
"juniper.gz"_
$result1 _
$result2;

 

 

Highlighted
Junos

Re: help with op script that save configuration file with hostname and data time

‎03-03-2020 02:54 AM

ok thanks!

Feedback