Junos OS

Expand all | Collapse all

help with op script that save configuration file in remote server

Jump to Best Answer
  • 1.  help with op script that save configuration file in remote server

    Posted 03-09-2020 07:54

    Hi all,

    I'm trying to write a script that allows a user to save the configuration on a backup server when needed.

    if I use the script to save with a local path it works.
    If I try to use a remote path it doesn't work, but it doesn't even leave me errors.

    What am I doing wrong.....

     

    this is my 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 $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 remote host\n" _ $local-out;

    var $year = substring( $localtime-iso, 1, 4 );
    var $month= substring( $localtime-iso, 6, 2 );
    var $day = substring( $localtime-iso, 9, 2 );

    var $file-copy-rpc=<file-copy>{
    <source>"/config/juniper.conf.gz";
    <destination>"scp://user:password@1.1.1.1/bkdirectory/" _ $hostname _ "_" _ $year _ $month _ $day _ "_juniper.conf.gz";
    }
    var $results=jcs:invoke($file-copy-rpc);

    }
    }

     

     

    thanks in advances

     

    Simone



  • 2.  RE: help with op script that save configuration file in remote server

     
    Posted 03-09-2020 08:16

    Hi,

     

    Can you please use FTP instead of SCP in the line shown below. I think FTP should work

     

    "scp://userRobot tongueassword@1.1.1.1/bkdirectory/" _ $hostname _ "_" _ $year _ $month _ $day _ "_juniper.conf.gz";

     

    https://www.juniper.net/documentation/en_US/junos/topics/reference/command-summary/file-copy.html

     

    PS: If my response solves your query please accept it as solutuion, kuods are appreciated too!

     

    Thanks

    Vishal



  • 3.  RE: help with op script that save configuration file in remote server

    Posted 03-10-2020 06:37

    I'm sorry but for security reason I can use only scp or sftp .

     

    can you do this on command as you can do with the option

    set system archival configuration archive-sites " scp://user@1.1.1.1/bkdirectory" password xxxxxxxx ?

     

     



  • 4.  RE: help with op script that save configuration file in remote server
    Best Answer

    Posted 03-20-2020 06:55

    This is my finaly solution:

     

    ######################################################

    #these imports are for ssh, date time and juniper info.

    from junos import Junos_Context
    import paramiko
    from datetime import datetime
    import jcs

    user = Junos_Context['user-context']['login-name']
    hostname = Junos_Context['hostname']
    now = datetime.now()
    day = now.strftime('%Y%m%d')
    hour = now.strftime('%H%M%S')

    #these are used to enter remote server ip, login and password

    host = '10.0.0.1'
    login = 'mylogin'
    passw = 'mypassword'


    #Sets up the ssh session and logs in as login "mylogin" with password "mypassword"
    #to host '10.0.0.1'
    #Also added "look_for_keys=False" and "allow_agent=False".
    #Hopefully this is clear, if we can't establish a connection, we'll set "chan" to false.
    #otherwise the configuration file is saved in the defined folder of the server
    #with all the data of who saved the file and when.
    #"All it's ok" message when done.

    try:
    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ssh.connect(host, username=login, password=passw, look_for_keys=False, allow_agent=False)
    chan = ssh.invoke_shell()
    except:
    print "Login to %s failed" % (host,)
    chan = False


    if chan:
    sftp = ssh.open_sftp()
    sftp.put('/config/juniper.conf.gz','/backups/config/%s_%s_%s_%s_juniper.conf.gz' % (user,hostname,day,hour))
    sftp.close()

    ssh.close()

    print "All it's OK %s ! " % (user,)

    else:
    print "Sorry, there is no connection to the host %s" % (host,)

    #####################################end##############################

    thanks all!