Help us improve your experience.

Let us know what you think.

Do you have time for a two-minute survey?

Announcement: Try the Ask AI chatbot for answers to your technical questions about Juniper products and solutions.

close
header-navigation
keyboard_arrow_up
close
keyboard_arrow_left
Junos PyEZ Developer Guide
Table of Contents Expand all
list Table of Contents
file_download PDF
{ "lLangCode": "en", "lName": "English", "lCountryCode": "us", "transcode": "en_US" }
English
keyboard_arrow_right

Use Junos PyEZ to Perform File System Operations

date_range 20-May-24

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:

content_copy zoom_out_map
from jnpr.junos import Device
from jnpr.junos.utils.fs import FS
from pprint import pprint

with  Device(host='router1.example.net') as dev:
    fs = FS(dev)
    pprint (fs.ls(path='/var/db/scripts/commit'))
content_copy zoom_out_map
user@server:~$ python3 junos-pyez-file-ls.py 
{'file_count': 3,
 'files': {'filter_type_check.py': {'owner': 'root',
                                    'path': 'filter_type_check.py',
                                    'permissions': 771,
                                    'permissions_text': '-rwxrwx--x',
                                    'size': 30070,
                                    'ts_date': 'Oct 25 03:24',
                                    'ts_epoc': '1698229487',
                                    'type': 'file'},
           'services': {'owner': 'root',
                        'path': 'services',
                        'permissions': 771,
                        'permissions_text': 'drwxrwx--x',
                        'size': 4096,
                        'ts_date': 'Oct 25 03:25',
                        'ts_epoc': '1698229535',
                        'type': 'dir'},
           'services_evpn_commit_script.py': {'owner': 'root',
                                              'path': 'services_evpn_commit_script.py',
                                              'permissions': 771,
                                              'permissions_text': '-rwxrwx--x',
                                              'size': 698,
                                              'ts_date': 'Oct 25 03:25',
                                              'ts_epoc': '1698229535',
                                              'type': 'file'}},
 'path': '/var/db/scripts/commit',
 'size': 34864,
 'type': 'dir'}

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.

content_copy zoom_out_map
from jnpr.junos import Device
from jnpr.junos.utils.fs import FS

with  Device(host='router1.example.net') as dev:
    fs = FS(dev)
    filepath = '/var/db/scripts/commit/filter_type_check.py'
    print(fs.cat(path=filepath))

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.

content_copy zoom_out_map
from jnpr.junos import Device
from jnpr.junos.utils.fs import FS

src='/var/tmp/bgp-neighbors.slax'
dest='/var/db/scripts/op'

with  Device(host='router1.example.net') as dev:
    fs = FS(dev)
    print(fs.cp(from_path=src, to_path=dest))

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:

content_copy zoom_out_map
fs.cp(from_path=src, to_path=dest, routing_instance='mgmt_junos')

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:

content_copy zoom_out_map
from jnpr.junos import Device
from jnpr.junos.utils.fs import FS

with  Device(host='router1.example.net') as dev:
    fs = FS(dev)

    # if file exists, calculate checksum
    filepath = '/var/db/scripts/commit/filter_type_check.py'
    if fs.cat(path=filepath) is not None:
        print(fs.checksum(path=filepath, calc='sha256'))
    else:
        print('File not found.')
content_copy zoom_out_map
user@server:~$ python3 junos-pyez-file-checksum.py
d5e28ddde10a38b247d491b524b59c022297b01a9d3a00b83b11be5eb7b81be3

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:

content_copy zoom_out_map
from jnpr.junos import Device
from jnpr.junos.utils.fs import FS
from pprint import pprint

with  Device(host='router1.example.net') as dev:
    fs = FS(dev)
    pprint(fs.storage_usage())
content_copy zoom_out_map
user@server:~$ python3 junos-pyez-storage-usage.py
{'/dev/ada1p2': {'avail': '406M',
                 'avail_block': 831176,
                 'mount': '/.mount/var/log',
                 'total': '475M',
                 'total_blocks': 973368,
                 'used': '31M',
                 'used_blocks': 64328,
                 'used_pct': '7'},
 '/dev/ada1p3': {'avail': '833M',
                 'avail_block': 1705144,
                 'mount': '/.mount/var/tmp',
                 'total': '2.7G',
                 'total_blocks': 5586168,
                 'used': '1.6G',
                 'used_blocks': 3434136,
                 'used_pct': '67'},
 '/dev/gpt/junos': {'avail': '4.3G',
                    'avail_block': 8989740,
                    'mount': '/.mount',
                    'total': '6.0G',
                    'total_blocks': 12540924,
                    'used': '1.2G',
                    'used_blocks': 2547912,
                    'used_pct': '22'},
...
}

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.

content_copy zoom_out_map
from jnpr.junos import Device
from jnpr.junos.utils.fs import FS

with  Device(host='router1.example.net') as dev:
    fs = FS(dev)
    print(fs.directory_usage(path='/var/tmp/'))
content_copy zoom_out_map
user@server:~$ python3 junos-pyez-directory-usage.py
{'/var/tmp/': {'size': '16K', 'blocks': 32, 'bytes': 16384}}

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.

content_copy zoom_out_map
from jnpr.junos.utils.fs import FS
from pprint import pprint

with  Device(host='router1.example.net') as dev:
    fs = FS(dev)
    print('\n*** Cleanup Check - files to delete ***\n')
    pprint(fs.storage_cleanup_check())

    cleanup = input('\nProceed with storage cleanup '
                    'and delete these files [yes,no] (no) ? ').lower()
    if cleanup in ['yes', 'y']:
        print('Cleaning up storage.')
        files = fs.storage_cleanup()
        pprint(files)
    elif cleanup in ['no', 'n', '']:
        print('Cleanup operation canceled.')
    else:
        print('Please enter a valid response.')
content_copy zoom_out_map
user@server:~$ python3 junos-pyez-storage-cleanup.py 

*** Cleanup Check - files to delete ***

{'/var/log/ifstraced.0.gz': {'size': 8474516, 'ts_date': 'Feb  7 09:12'},
 '/var/log/ifstraced.1.gz': {'size': 8464409, 'ts_date': 'Feb  6 19:17'},
 '/var/log/ifstraced.2.gz': {'size': 8476558, 'ts_date': 'Feb  6 05:19'},
 '/var/log/ifstraced.3.gz': {'size': 8477741, 'ts_date': 'Feb  5 15:20'},
 '/var/log/license.0.gz': {'size': 27591, 'ts_date': 'Feb  7 17:03'},
 '/var/log/license.1.gz': {'size': 27802, 'ts_date': 'Feb  7 15:11'},
 '/var/log/messages.0.gz': {'size': 189, 'ts_date': 'Feb  7 17:53'},
 '/var/log/messages.1.gz': {'size': 173, 'ts_date': 'Feb  7 17:52'},
 '/var/log/messages.2.gz': {'size': 128, 'ts_date': 'Feb  7 17:52'},
 '/var/log/messages.3.gz': {'size': 190, 'ts_date': 'Feb  7 17:52'},
 '/var/log/messages.4.gz': {'size': 173, 'ts_date': 'Feb  7 17:48'},
 '/var/log/security.0.gz': {'size': 499, 'ts_date': 'Feb  7 17:53'},
 '/var/log/security.1.gz': {'size': 358, 'ts_date': 'Feb  7 17:52'},
 '/var/log/security.2.gz': {'size': 305, 'ts_date': 'Feb  7 17:52'},
 '/var/log/security.3.gz': {'size': 536, 'ts_date': 'Feb  7 17:52'},
 '/var/log/wtmp.0.gz': {'size': 27, 'ts_date': 'Feb  7 17:52'},
 '/var/log/wtmp.1.gz': {'size': 27, 'ts_date': 'Feb  7 17:52'},
 '/var/tmp/LOCK_FILE': {'size': 0, 'ts_date': 'Jan  2 11:08'},
 '/var/tmp/appidd_cust_app_trace': {'size': 0, 'ts_date': 'Jan  2 11:08'},
 '/var/tmp/appidd_trace_debug': {'size': 198, 'ts_date': 'Jan  2 11:08'},
 '/var/tmp/krt_rpf_filter.txt': {'size': 57, 'ts_date': 'Jan  2 11:08'},
 '/var/tmp/netproxy': {'size': 0, 'ts_date': 'Jan  2 11:11'},
 '/var/tmp/pfe_debug_commands': {'size': 111, 'ts_date': 'Jan  2 11:08'},
 '/var/tmp/planeDb.log': {'size': 524, 'ts_date': 'Jan  2 11:08'},
 '/var/tmp/rtsdb/if-rtsdb': {'size': 0, 'ts_date': 'Jan  2 11:08'}}

Proceed with storage cleanup and delete these files [yes,no] (no) ? yes
Cleaning up storage.
{'/var/log/ifstraced.0.gz': {'size': 8474516, 'ts_date': 'Feb  7 09:12'},
 '/var/log/ifstraced.1.gz': {'size': 8464409, 'ts_date': 'Feb  6 19:17'},
 '/var/log/ifstraced.2.gz': {'size': 8476558, 'ts_date': 'Feb  6 05:19'},
 '/var/log/ifstraced.3.gz': {'size': 8477741, 'ts_date': 'Feb  5 15:20'},
 '/var/log/license.0.gz': {'size': 27591, 'ts_date': 'Feb  7 17:03'},
 '/var/log/license.1.gz': {'size': 27802, 'ts_date': 'Feb  7 15:11'},
 '/var/log/messages.0.gz': {'size': 130, 'ts_date': 'Feb  7 17:54'},
 '/var/log/messages.1.gz': {'size': 189, 'ts_date': 'Feb  7 17:53'},
 '/var/log/messages.2.gz': {'size': 173, 'ts_date': 'Feb  7 17:52'},
 '/var/log/messages.3.gz': {'size': 128, 'ts_date': 'Feb  7 17:52'},
 '/var/log/messages.4.gz': {'size': 190, 'ts_date': 'Feb  7 17:52'},
 '/var/log/security.1.gz': {'size': 499, 'ts_date': 'Feb  7 17:53'},
 '/var/log/security.2.gz': {'size': 358, 'ts_date': 'Feb  7 17:52'},
 '/var/log/security.3.gz': {'size': 305, 'ts_date': 'Feb  7 17:52'},
 '/var/log/wtmp.0.gz': {'size': 27, 'ts_date': 'Feb  7 17:53'},
 '/var/log/wtmp.1.gz': {'size': 27, 'ts_date': 'Feb  7 17:52'},
 '/var/tmp/LOCK_FILE': {'size': 0, 'ts_date': 'Jan  2 11:08'},
 '/var/tmp/appidd_cust_app_trace': {'size': 0, 'ts_date': 'Jan  2 11:08'},
 '/var/tmp/appidd_trace_debug': {'size': 198, 'ts_date': 'Jan  2 11:08'},
 '/var/tmp/krt_rpf_filter.txt': {'size': 57, 'ts_date': 'Jan  2 11:08'},
 '/var/tmp/netproxy': {'size': 0, 'ts_date': 'Jan  2 11:11'},
 '/var/tmp/pfe_debug_commands': {'size': 111, 'ts_date': 'Jan  2 11:08'},
 '/var/tmp/planeDb.log': {'size': 524, 'ts_date': 'Jan  2 11:08'},
 '/var/tmp/rtsdb/if-rtsdb': {'size': 0, 'ts_date': 'Jan  2 11:08'}}
footer-navigation