Copying Files Through SCP With Junos PyEZ | zeroslash.io

Copying Files Through SCP With Junos PyEZ

Submitted by zeroslash on Wed, 08/29/2018 - 15:52

Copying a file is one of the simplest tasks you can do with Junos PyEZ or with Junos in general. You typically do this for example when you are copying the text file for request support information before you submit it to support. Having said that, let's get right into an example of this using Junos PyEZ.

It's pretty straightforward as you can see. We use StartShell to run request support information and also have it saved locally into a file (lines 8-10).

 

In the previous example, we were just copying a simple file. But what if we want to copy a folder? We need to perform a recursive copy.

Notice that we are using the recursive option for scp get (line 8). It's worth noting that there is nothing special with the way we are using scp here. Junos PyEZ is just using the python scp module under the hood. This means that we are able to use the same optional parameters as we would have if we were using the scp module directly.

     |  get(self, remote_path, local_path='', recursive=False, preserve_times=False)
     |      Transfer files and directories from remote host to localhost.
     |      
     |      @param remote_path: path to retrieve from remote host. since this is
     |          evaluated by scp on the remote host, shell wildcards and
     |          environment variables may be used.
     |      @type remote_path: str
     |      @param local_path: path in which to receive files locally
     |      @type local_path: str
     |      @param recursive: transfer files and directories recursively
     |      @type recursive: bool
     |      @param preserve_times: preserve mtime and atime of transferred files
     |          and directories.
     |      @type preserve_times: bool

We also introduced the progress option which enables the output of the progress while copying (line 7).

 

As another example, let's now try to copy the logs of the device and save it into a local folder.

Here we used the local_path option to specify where we want to save the file (line 8). We specified a folder plus a file name. Please note that the logs folder needs to exist.

Here's a sample output:

vsrx3: logs/vsrx3-messages: 49152 / 459484 (10%)
vsrx3: logs/vsrx3-messages: 139264 / 459484 (30%)
vsrx3: logs/vsrx3-messages: 278528 / 459484 (60%)
vsrx3: logs/vsrx3-messages: 459484 / 459484 (100%)

 

NOTE: You can also use scp put to copy files from you local machine to the remote Junos device as well.

Example code:

scp.put("myfile", remote_path="/var/tmp/myfile")

Example output:

vsrx3: b'myfile': 0 / 24 (0%)
vsrx3: b'myfile': 24 / 24 (100%)

 

We can also send the progress output to our a log file. Yes, let's copy the logs from the Junos device and then create a log that we were able to copy the log file :)

The most significant part here is the log function (lines 5-9). We take this log function and pass it to the progress parameter of SCP (line 14). What will happen is we have the opportunity to catch the progress output from SCP (report variable) and write them to our own log file, or basically do whatever we want. In this case what we did is, since we are going to write them to a log file, we added a timestamp using the datetime module (line 7) and we were able to format it as preferred (line 8). You can choose whatever date time format you want, but I just went with the date time format as it is in the Junos logs. Finally, we prefixed our original progress output with our time stamp and wrote it to the log file (line 9).

Here's the sample logs:

Aug 30 07:29:05 vsrx3: logs/vsrx3-messages: 49152 / 461423 (10%)
Aug 30 07:29:05 vsrx3: logs/vsrx3-messages: 139264 / 461423 (30%)
Aug 30 07:29:05 vsrx3: logs/vsrx3-messages: 278528 / 461423 (60%)
Aug 30 07:29:05 vsrx3: logs/vsrx3-messages: 461423 / 461423 (100%)

 

In a production environment, you'll probably have only a few reasons to copy the log files because you would (or should) have a proper syslog server. However, this example along with the rest of the examples in this post are good enough to show what you can do with scp in Junos PyEZ.