Transferring Files Using Junos PyEZ


Junos PyEZ provides utilities that enable you to perform file management tasks on devices running Junos OS. The Junos PyEZ jnpr.junos.utils.scp.SCP class can be used to secure copy (SCP) files between the local host and a device running Junos OS. Instances of the SCP class can be used as context managers. For example:

Starting in Junos PyEZ Release 1.2.3, SCP introduces new ways to enable you to track the progress of transfers using the progress parameter. By default, SCP does not print progress messages. Set progress=True to print default progress messages at transfer completion intervals of 10 percent or greater.

Alternatively, you can define a custom function to print progress messages, and then set the progress parameter equal to the name of the function. The function definition should include two parameters corresponding to the device instance and the progress message. For example:

The following sample program transfers the scp-test1.txt and scp-test2.txt files from the local host to the /var/tmp directory on the target device, and then transfers the messages log file from the target device to a logs directory on the local host. The messages log is renamed to append the device hostname to the filename. The example uses SSH keys, which are already configured on the local host and the device, for authentication.

For comparison purposes, the program uses both the default progress messages as well as custom messages, which are defined in the function named log, to track the progress of the transfers.

The progress of the transfers is sent to standard output. The default output (progress=True) includes the device name, the file being transferred, and the progress of the transfer in both bytes and as a percentage.

The custom function produces similar output in this case.

After executing the program, you can verify that the scp-test1.txt and scp-test2.txt files were copied to the correct directory on the target device by issuing the file list command on the device.

user1@router1> file list /var/tmp/scp-test*

On the local host, the messages log file, which is renamed to include the device hostname, should be present in the logs directory.

[user1@localhost ~]$ ls logs

The SCP class provides support for ProxyCommand, which enables you to transfer files from the local host to the target device through an intermediary host that supports netcat. This is useful when you can only log in to the target device through the intermediate host. To configure ProxyCommand, add the appropriate information to the SSH configuration file. For example:

By default, Junos PyEZ queries the default SSH configuration file at ~/.ssh/config, if one exists. However, you can specify a different SSH configuration file when you create the device instance by including the ssh_config parameter in the Device argument list.

Starting in Junos PyEZ Release 2.0.1, when you include the ssh_private_key_file parameter in the Device argument list to define a specific SSH private key file for authentication, the SCP instance uses the same key file for authentication when transferring files.