-
Notifications
You must be signed in to change notification settings - Fork 52
Add file_pull option to Cisco IOS devices #345
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from all commits
fb6efa6
75ad842
73f74e6
bedf623
c1261eb
8ac24d3
aa31915
c314ac6
431b789
59cdebb
af118a1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Added the ability to download files from within a Cisco IOS device. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,11 @@ | ||
| """The module contains the base class that all device classes must inherit from.""" | ||
|
|
||
| import hashlib | ||
| import importlib | ||
| import warnings | ||
|
|
||
| from pyntc.errors import FeatureNotFoundError, NTCError | ||
| from pyntc.utils.models import FileCopyModel | ||
|
|
||
|
|
||
| def fix_docs(cls): | ||
|
|
@@ -221,7 +223,7 @@ def file_copy(self, src, dest=None, **kwargs): | |
|
|
||
| Keyword Args: | ||
| file_system (str): Supported only for IOS and NXOS. The file system for the | ||
| remote fle. If no file_system is provided, then the ``get_file_system`` | ||
| remote file. If no file_system is provided, then the ``get_file_system`` | ||
| method is used to determine the correct file system to use. | ||
| """ | ||
| raise NotImplementedError | ||
|
|
@@ -241,13 +243,126 @@ def file_copy_remote_exists(self, src, dest=None, **kwargs): | |
|
|
||
| Keyword Args: | ||
| file_system (str): Supported only for IOS and NXOS. The file system for the | ||
| remote fle. If no file_system is provided, then the ``get_file_system`` | ||
| remote file. If no file_system is provided, then the ``get_file_system`` | ||
| method is used to determine the correct file system to use. | ||
|
|
||
| Returns: | ||
| (bool): True if the remote file exists, False if it doesn't. | ||
| """ | ||
|
|
||
| def check_file_exists(self, filename, **kwargs): | ||
| """Check if a remote file exists by filename. | ||
|
|
||
| Args: | ||
| filename (str): The name of the file to check for on the remote device. | ||
| kwargs (dict): Additional keyword arguments that may be used by subclasses. | ||
|
|
||
| Keyword Args: | ||
| file_system (str): Supported only for IOS and NXOS. The file system for the | ||
| remote file. If no file_system is provided, then the ``get_file_system`` | ||
| method is used to determine the correct file system to use. | ||
|
|
||
| Returns: | ||
| (bool): True if the remote file exists, False if it doesn't. | ||
| """ | ||
| raise NotImplementedError | ||
|
|
||
| def get_remote_checksum(self, filename, hashing_algorithm="md5", **kwargs): | ||
| """Get the checksum of a remote file. | ||
|
|
||
| Args: | ||
| filename (str): The name of the file to check for on the remote device. | ||
| hashing_algorithm (str): The hashing algorithm to use (default: "md5"). | ||
| kwargs (dict): Additional keyword arguments that may be used by subclasses. | ||
|
|
||
| Keyword Args: | ||
| file_system (str): Supported only for IOS and NXOS. The file system for the | ||
| remote file. If no file_system is provided, then the ``get_file_system`` | ||
| method is used to determine the correct file system to use. | ||
|
|
||
| Returns: | ||
| (str): The checksum of the remote file. | ||
| """ | ||
| raise NotImplementedError | ||
|
|
||
| @staticmethod | ||
| def get_local_checksum(filepath, hashing_algorithm="md5", add_newline=False): | ||
| """Get the checksum of a local file using a specified algorithm. | ||
|
|
||
| Args: | ||
| filepath (str): The path to the local file. | ||
| hashing_algorithm (str): The hashing algorithm to use (e.g., "md5", "sha256"). | ||
| add_newline (bool): Whether to append a newline before final hashing (Some devices may require this). | ||
|
|
||
| Returns: | ||
| (str): The hex digest of the file. | ||
| """ | ||
| # Initialize the hash object dynamically | ||
| file_hash = hashlib.new(hashing_algorithm.lower()) | ||
|
|
||
| with open(filepath, "rb") as f: | ||
| # Read in chunks to handle large firmware files without RAM spikes | ||
| for chunk in iter(lambda: f.read(4096), b""): | ||
| file_hash.update(chunk) | ||
|
|
||
| if add_newline: | ||
| file_hash.update(b"\n") | ||
|
|
||
| return file_hash.hexdigest() | ||
|
|
||
| def compare_file_checksum(self, checksum, filename, hashing_algorithm="md5", **kwargs): | ||
| """Compare the checksum of a local file with a remote file. | ||
|
|
||
| Args: | ||
| checksum (str): The checksum of the file. | ||
| filename (str): The name of the file to check for on the remote device. | ||
| hashing_algorithm (str): The hashing algorithm to use (default: "md5"). | ||
| kwargs (dict): Additional keyword arguments that may be used by subclasses. | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this need the |
||
|
|
||
| Keyword Args: | ||
| file_system (str): Supported only for IOS and NXOS. The file system for the | ||
| remote file. If no file_system is provided, then the ``get_file_system`` | ||
| method is used to determine the correct file system to use. | ||
|
|
||
| Returns: | ||
| (bool): True if the checksums match, False otherwise. | ||
| """ | ||
| return checksum == self.get_remote_checksum(filename, hashing_algorithm, **kwargs) | ||
|
|
||
| def remote_file_copy(self, src: FileCopyModel = None, dest=None, **kwargs): | ||
| """Copy a file to a remote device. | ||
|
|
||
| Args: | ||
| src (FileCopyModel): The source file model. | ||
| dest (str): The destination file path on the remote device. | ||
| kwargs (dict): Additional keyword arguments that may be used by subclasses. | ||
|
|
||
| Keyword Args: | ||
| file_system (str): Supported only for IOS and NXOS. The file system for the | ||
| remote file. If no file_system is provided, then the ``get_file_system`` | ||
| method is used to determine the correct file system to use. | ||
| """ | ||
| raise NotImplementedError | ||
|
|
||
| def verify_file(self, checksum, filename, hashing_algorithm="md5", **kwargs): | ||
| """Verify a file on the remote device by confirming the file exists and validate the checksum. | ||
|
|
||
| Args: | ||
| checksum (str): The checksum of the file. | ||
| filename (str): The name of the file to check for on the remote device. | ||
| hashing_algorithm (str): The hashing algorithm to use (default: "md5"). | ||
| kwargs (dict): Additional keyword arguments that may be used by subclasses. | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this need the
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like it should. I will add. |
||
|
|
||
| Keyword Args: | ||
| file_system (str): Supported only for IOS and NXOS. The file system for the | ||
| remote file. If no file_system is provided, then the ``get_file_system`` | ||
| method is used to determine the correct file system to use. | ||
|
|
||
| Returns: | ||
| (bool): True if the file is verified successfully, False otherwise. | ||
| """ | ||
| raise NotImplementedError | ||
|
|
||
| def install_os(self, image_name, **vendor_specifics): | ||
| """Install the OS from specified image_name. | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did this come from netmiko? Seems weird but if it's in netmiko it's probably fixing some strange quirk in a device
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is in Netmiko, which is where it came from, but doesn't seem to be used for IOS. Probably fixing something in some devices.