Genie-libs FileTransferUtils
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
-
AttributeError: 'NoneType' object has no attribute 'os'
cause The `device` object passed to `FileTransferUtils` was `None` or was not properly initialized by a pyATS testbed.fixEnsure your pyATS testbed is loaded and you are passing a valid `Device` instance to `FileTransferUtils(device=my_device)`. -
FileNotFoundError: [Errno 2] No such file or directory: '/path/to/local/non_existent_file.txt'
cause The `source_path` provided to `prepare_transfer` or `transfer` does not exist on the local filesystem.fixVerify that `source_path` points to an existing file on the machine running the script. Use `os.path.exists(source_path)` to check before calling the utility. -
TypeError: prepare_transfer() missing 1 required positional argument: 'destination_path'
cause Required arguments like `source_path` or `destination_path` were not provided to `prepare_transfer` or `transfer` methods.fixAlways provide `source_path` and `destination_path` arguments, along with `method` (e.g., 'scp', 'tftp'), to the `prepare_transfer` and `transfer` methods.
Warnings
- gotcha FileTransferUtils relies heavily on a properly initialized `device` object from a pyATS testbed. If the `device` object is `None` or lacks necessary attributes (e.g., `os`, `connections`), methods will fail.
- gotcha Version mismatches between `genie-libs-filetransferutils` and other core `genie` or `pyats` packages can lead to unexpected `AttributeError` or import failures, as the ecosystem is tightly integrated.
- gotcha When performing file transfers, ensure both the local source file exists and the remote destination path on the device is valid and has sufficient permissions/space. Common issues include `FileNotFoundError` (for local source) or device-side errors (permissions, invalid path) reported by the transfer method.
Install
-
pip install genie-libs-filetransferutils
Imports
- FileTransferUtils
from genie.libs.filetransferutils.filetransferutils import FileTransferUtils
Quickstart
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)