Help us improve your experience.

Let us know what you think.

Do you have time for a two-minute survey?

 
 

Transfer Files Using Junos PyEZ

SUMMARY Use Junos PyEZ to secure copy (SCP) files between the local host and a Junos device.

Junos PyEZ provides utilities that enable you to perform file management tasks on Junos devices. You can use the Junos PyEZ jnpr.junos.utils.scp.SCP class to secure copy (SCP) files between the local host and a Junos device.

The SCP open() and close() methods establish and terminate the connection with the device. As a result, if the client application only performs file copy operations, it can omit calls to the Device open() and close() methods. Instances of the SCP class can be used as context managers, which automatically call the open() and close() methods. For example:

SCP enables 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, issue the file list command on the target device to verify that the scp-test1.txt and scp-test2.txt files were copied to the correct directory.

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

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. For example:

Additionally, 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.

The SCP class also 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: