|
5 | 5 |
|
6 | 6 | """ |
7 | 7 |
|
8 | | -from typing import Any |
| 8 | +import os |
| 9 | +from typing import Any, Optional |
| 10 | + |
9 | 11 | from netmiko.cisco_base_connection import CiscoSSHConnection |
| 12 | +from netmiko.scp_handler import BaseFileTransfer |
10 | 13 |
|
11 | 14 |
|
12 | 15 | class ArubaOsSSH(CiscoSSHConnection): |
@@ -52,3 +55,152 @@ def config_mode( |
52 | 55 | return super().config_mode( |
53 | 56 | config_command=config_command, pattern=pattern, re_flags=re_flags |
54 | 57 | ) |
| 58 | + |
| 59 | + |
| 60 | +class ArubaOsFileTransfer(BaseFileTransfer): |
| 61 | + """Aruba OS SCP File Transfer driver""" |
| 62 | + |
| 63 | + def __init__( |
| 64 | + self, |
| 65 | + file_system: Optional[str] = "/mm/mynode", |
| 66 | + hash_supported: bool = False, |
| 67 | + **kwargs: Any, |
| 68 | + ) -> None: |
| 69 | + super().__init__( |
| 70 | + file_system=file_system, hash_supported=hash_supported, **kwargs |
| 71 | + ) |
| 72 | + |
| 73 | + def file_md5(self, file_name: str, add_newline: bool = False) -> str: |
| 74 | + msg = "Aruba OS does not support an MD5-hash operation." |
| 75 | + raise AttributeError(msg) |
| 76 | + |
| 77 | + @staticmethod |
| 78 | + def process_md5(md5_output: str, pattern: str = "") -> str: |
| 79 | + msg = "Aruba OS does not support an MD5-hash operation." |
| 80 | + raise AttributeError(msg) |
| 81 | + |
| 82 | + def compare_md5(self) -> bool: |
| 83 | + msg = "Aruba OS does not support an MD5-hash operation." |
| 84 | + raise AttributeError(msg) |
| 85 | + |
| 86 | + def remote_md5(self, base_cmd: str = "", remote_file: Optional[str] = None) -> str: |
| 87 | + msg = "Aruba OS does not support an MD5-hash operation." |
| 88 | + raise AttributeError(msg) |
| 89 | + |
| 90 | + def check_file_exists(self, remote_cmd: str = "") -> bool: |
| 91 | + """Check if the dest_file already exists on the file system (return boolean).""" |
| 92 | + if self.direction == "put": |
| 93 | + if not remote_cmd: |
| 94 | + remote_cmd = f"dir search {self.dest_file.rpartition('/')[-1]}" |
| 95 | + remote_out = self.ssh_ctl_chan._send_command_str(remote_cmd) |
| 96 | + |
| 97 | + if "Cannot get directory information" in remote_out: |
| 98 | + return False |
| 99 | + |
| 100 | + return self.dest_file in [ |
| 101 | + split_line[-1] |
| 102 | + for line in remote_out.splitlines() |
| 103 | + if (split_line := line.split()) |
| 104 | + ] |
| 105 | + elif self.direction == "get": |
| 106 | + return os.path.exists(self.dest_file) |
| 107 | + else: |
| 108 | + raise ValueError("Unexpected value for self.direction") |
| 109 | + |
| 110 | + def remote_file_size( |
| 111 | + self, remote_cmd: str = "", remote_file: Optional[str] = None |
| 112 | + ) -> int: |
| 113 | + """Get the file size of the remote file.""" |
| 114 | + if remote_file is None: |
| 115 | + if self.direction == "put": |
| 116 | + remote_file = self.dest_file |
| 117 | + elif self.direction == "get": |
| 118 | + remote_file = self.source_file |
| 119 | + |
| 120 | + assert isinstance(remote_file, str) |
| 121 | + remote_file_search = remote_file.rpartition("/")[-1] |
| 122 | + |
| 123 | + if not remote_cmd: |
| 124 | + remote_cmd = f"dir search {remote_file_search}" |
| 125 | + remote_out = self.ssh_ctl_chan._send_command_str(remote_cmd) |
| 126 | + |
| 127 | + if "Cannot get directory information" in remote_out: |
| 128 | + msg = "Unable to find file on remote system" |
| 129 | + raise IOError(msg) |
| 130 | + |
| 131 | + file_size = [ |
| 132 | + split_line[-5] |
| 133 | + for line in remote_out.splitlines() |
| 134 | + if (split_line := line.split()) and split_line[-1] == remote_file_search |
| 135 | + ] |
| 136 | + |
| 137 | + if len(file_size) != 1: |
| 138 | + msg = ( |
| 139 | + "Unable to parse remote file size, found file count is not equal to one" |
| 140 | + ) |
| 141 | + raise IOError(msg) |
| 142 | + |
| 143 | + try: |
| 144 | + return int(file_size[0]) |
| 145 | + except ValueError as ve: |
| 146 | + msg = "Unable to parse remote file size, wrong field in use or malformed command output" |
| 147 | + raise IOError(msg) from ve |
| 148 | + |
| 149 | + def verify_file(self) -> bool: |
| 150 | + """Verify the file has been transferred correctly based on filesize.""" |
| 151 | + if self.direction == "put": |
| 152 | + return os.stat(self.source_file).st_size == self.remote_file_size( |
| 153 | + remote_file=self.dest_file |
| 154 | + ) |
| 155 | + elif self.direction == "get": |
| 156 | + return ( |
| 157 | + self.remote_file_size(remote_file=self.source_file) |
| 158 | + == os.stat(self.dest_file).st_size |
| 159 | + ) |
| 160 | + else: |
| 161 | + raise ValueError("Unexpected value of self.direction") |
| 162 | + |
| 163 | + def remote_space_available(self, search_pattern: str = "") -> int: |
| 164 | + """Return space available on remote device.""" |
| 165 | + remote_cmd = "show storage" |
| 166 | + remote_output = self.ssh_ctl_chan._send_command_str(remote_cmd).strip() |
| 167 | + |
| 168 | + # df -h ouput |
| 169 | + available_sizes = [ |
| 170 | + split_line[-3] |
| 171 | + for line in remote_output.splitlines() |
| 172 | + if (split_line := line.split()) and split_line[-1] == "/flash" |
| 173 | + ] |
| 174 | + |
| 175 | + if not available_sizes: |
| 176 | + msg = "Could not determine remote space available." |
| 177 | + raise ValueError(msg) |
| 178 | + |
| 179 | + space_available = 0 |
| 180 | + |
| 181 | + for available_size in available_sizes: |
| 182 | + size_names = ["B", "K", "M", "G", "T", "P", "E", "Z", "Y"] |
| 183 | + |
| 184 | + size_str, suffix = available_size[:-1], available_size[-1] |
| 185 | + |
| 186 | + if suffix not in size_names: |
| 187 | + msg = "Could not determine remote space available." |
| 188 | + raise ValueError(msg) |
| 189 | + |
| 190 | + try: |
| 191 | + size = float(size_str) |
| 192 | + except ValueError as ve: |
| 193 | + msg = "Could not determine remote space available." |
| 194 | + raise ValueError(msg) from ve |
| 195 | + |
| 196 | + space_available += size * 1024 ** size_names.index(suffix) |
| 197 | + |
| 198 | + return int(space_available) |
| 199 | + |
| 200 | + def enable_scp(self, cmd: str = "service scp") -> None: |
| 201 | + """Enable SCP on remote device.""" |
| 202 | + super().enable_scp(cmd) |
| 203 | + |
| 204 | + def disable_scp(self, cmd: str = "no service scp") -> None: |
| 205 | + """Disable SCP on remote device.""" |
| 206 | + super().disable_scp(cmd) |
0 commit comments