Junos OS

Expand all | Collapse all

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

Jump to Best Answer
  • 1.  help with op script that save configuration file with hostname and data time

    Posted 03-02-2020 05:21

    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

     

     



  • 2.  RE: help with op script that save configuration file with hostname and data time

     
    Posted 03-02-2020 21:13

    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



  • 3.  RE: help with op script that save configuration file with hostname and data time

    Posted 03-03-2020 01:19

    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

     



  • 4.  RE: help with op script that save configuration file with hostname and data time

     
    Posted 03-03-2020 01:45

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



  • 5.  RE: help with op script that save configuration file with hostname and data time

    Posted 03-03-2020 02:36

    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



  • 6.  RE: help with op script that save configuration file with hostname and data time
    Best Answer

     
    Posted 03-03-2020 02:49

    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;

     

     



  • 7.  RE: help with op script that save configuration file with hostname and data time

    Posted 03-03-2020 02:55

    ok thanks!