Genie-libs FileTransferUtils

26.3 · active · verified Thu Apr 16

Genie-libs FileTransferUtils provides a set of utilities for managing file transfers to and from network devices within the Cisco Genie automation framework. It simplifies operations like preparing a device for transfer, initiating file uploads/downloads, and verifying file integrity. The current version is 26.3, and it follows the release cadence of the broader Genie/pyATS ecosystem, typically with frequent updates aligned with new Genie releases.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `FileTransferUtils` to prepare and execute a file transfer to a network device. It uses `unittest.mock.MagicMock` to simulate a `device` object from a pyATS testbed, allowing the code to run without a live network setup. In a real scenario, the `device` object would be obtained from a loaded pyATS testbed (`pyats.topology.loader`).

import os
from unittest.mock import MagicMock
from genie.libs.filetransferutils.filetransferutils import FileTransferUtils

# Mock a device object for demonstration purposes
# In a real scenario, this 'device' object would come
# from a loaded pyATS testbed (e.g., testbed.devices['my_device'])
mock_device = MagicMock()
mock_device.name = "mock_device_nxos1"
mock_device.connections.cli.connection_args = {'protocol': 'ssh'}
mock_device.os = 'nxos'

# Mock FileTransferUtils methods that would interact with a real device
mock_file_transfer_obj = MagicMock()
mock_file_transfer_obj.transfer.return_value = True # Simulate successful transfer
mock_file_transfer_obj.get_file_size.return_value = 1024 # Simulate file size check
mock_device.api.get_file_transfer_obj.return_value = mock_file_transfer_obj

# Create a dummy local file for transfer simulation
local_file_path = "local_dummy_file.txt"
with open(local_file_path, "w") as f:
    f.write("This is a dummy file for transfer simulation.")

try:
    # Initialize FileTransferUtils with the device object
    file_transfer = FileTransferUtils(device=mock_device)

    # Define remote path for the transfer
    remote_path = "/bootflash/dummy_file_on_device.txt"

    # Prepare for file transfer (e.g., check space, permissions on the device)
    # The 'prepare_transfer' method returns a dictionary of transfer details
    transfer_info = file_transfer.prepare_transfer(
        source_path=local_file_path,
        destination_path=remote_path,
        method="scp", # Common transfer methods: scp, tftp, ftp
        overwrite=True
    )

    print(f"Prepared transfer for device {mock_device.name}: {transfer_info}")

    # Perform the actual file transfer to the device
    success = file_transfer.transfer(
        source_path=local_file_path,
        destination_path=remote_path,
        method="scp",
        overwrite=True
    )

    if success:
        print(f"File '{local_file_path}' successfully transferred to '{remote_path}' on {mock_device.name}")
        # Example: Get remote file size (mocked interaction)
        remote_file_size = file_transfer.get_file_size(path=remote_path, method="scp")
        print(f"Remote file size: {remote_file_size} bytes")
    else:
        print("File transfer failed.")

except Exception as e:
    print(f"An error occurred: {e}")
finally:
    # Clean up the dummy file
    if os.path.exists(local_file_path):
        os.remove(local_file_path)

view raw JSON →