Help us improve your experience.

Let us know what you think.

Do you have time for a two-minute survey?

 
 

Use Junos PyEZ to Perform File System Operations

SUMMARY Use Junos PyEZ to manage files and directories, calculate checksums, and view and clean up system storage on Junos devices.

Junos PyEZ applications can use the jnpr.junos.utils.fs utility to perform file system operations on Junos devices, including:

  • Managing files

  • Managing directories

  • Managing disk space

This topic discusses how to use the utility to perform some common file system operations.

Perform File Operations

You can use the jnpr.junos.utils.fs utility to perform common file and directory operations. For example, you can:

  • View file and directory information

  • Create and delete directories

  • View, delete, and move or copy files

  • Calculate a file's checksum

This section discusses some common operations. For the full list of supported operations, see the jnpr.junos.utils.fs documentation.

View File Listings

You can use the ls() method to view the list of files and directories at a given path. For example:

Manage Files

The jnpr.junos.utils.fs utility enables you to perform common file operations on the target device. Table 1 outlines the methods. The method name is identical to the corresponding command on Unix-like operating systems.

Table 1: Junos PyEZ File Operation Methods

Method

Description

cat()

View a file.

cp()

Copy a file.

mv()

Rename a file.

rm()

Delete a file.

The following Junos PyEZ application connects to a Junos device and uses the cat() method to view the contents of a file. If the file does not exist, the application prints None.

The following application connects to a Junos device and copies a file from the /var/tmp directory to the /var/db/scripts/op directory. The application prints True if the operation is successful or False if there is an error.

Starting in Junos PyEZ Release 2.6.8, you can specify a routing instance for the file copy operation. Include the routing_instance argument, and specify the name of the instance. For example:

Calculate Checksums

You can use the checksum() method to calculate the checksum of a file. By default, checksum() calculates the MD5 checksum. To explicitly specify the algorithm, include the calc argument and specify one of the following values:

  • md5

  • sha256

  • sha1

The following Junos PyEZ application connects to a Junos device, uses the cat() method to verify the existence of a file, and if the file exists, calculates the SHA256 checksum:

Manage File System Storage

You can use the jnpr.junos.utils.fs utility to manage file system storage as described in the following sections.

View File System Disk Space Usage

You can use the storage_usage() method to return information about a file system's used and available space. The method returns the output of the show system storage command. The information is similar to the Unix df command output.

The following Junos PyEZ application retrieves and prints the disk space usage for the connected device:

View Directory Usage

You can use the directory_usage() method to view the disk space usage for a given directory, and optionally, its subdirectories. This method executes the show system directory-usage path command on the device and returns the information. If you do not specify a depth, Junos PyEZ uses the default depth of zero. The information is similar to the statistics returned by the Unix du command.

The following Junos PyEZ application prints the disk space usage for the /var/tmp directory.

Clean Up System Storage

You can use the storage_cleanup() method to free up disk space on a Junos device. The method executes the request system storage cleanup command, which rotates log files and removes temporary files.

To only view the list of files that would be removed in the cleanup operation, use the storage_cleanup_check() method instead. This method executes the request system storage cleanup dry-run command on the device and returns the list of candidate files without deleting them.

The following Junos PyEZ application first executes the storage_cleanup_check operation and prints the list of files that are proposed for deletion. The application then queries if the user wants to proceed with the storage cleanup and delete the files. If the user confirms the cleanup operation, the application executes the storage_cleanup() operation to delete the files and then prints the list of deleted files.