|
| 1 | +import time |
| 2 | +import re |
| 3 | +from typing import Any |
| 4 | + |
| 5 | +from netmiko.base_connection import BaseConnection |
| 6 | +from netmiko.no_enable import NoEnable |
| 7 | + |
| 8 | + |
| 9 | +class NokiaIsamSSH(BaseConnection, NoEnable): |
| 10 | + |
| 11 | + def session_preparation(self) -> None: |
| 12 | + self._test_channel_read() |
| 13 | + self.set_base_prompt() |
| 14 | + commands = [ |
| 15 | + "environment inhibit-alarms", |
| 16 | + "environment screen-length 0", |
| 17 | + ] |
| 18 | + for command in commands: |
| 19 | + self.disable_paging(command=command, cmd_verify=True, pattern=r"#") |
| 20 | + time.sleep(0.3 * self.global_delay_factor) |
| 21 | + self.clear_buffer() |
| 22 | + |
| 23 | + def set_base_prompt(self, *args: Any, **kwargs: Any) -> str: |
| 24 | + """Remove the > when navigating into the different config level.""" |
| 25 | + cur_base_prompt = super().set_base_prompt(*args, **kwargs) |
| 26 | + match = re.search(r"\*?(.*?)(>.*)*#", cur_base_prompt) |
| 27 | + if match: |
| 28 | + # strip off >... from base_prompt; strip off leading * |
| 29 | + self.base_prompt: str = match.group(1) |
| 30 | + |
| 31 | + return self.base_prompt |
| 32 | + |
| 33 | + def cleanup(self, command: str = "logout") -> None: |
| 34 | + """Gracefully exit the SSH session.""" |
| 35 | + try: |
| 36 | + if self.check_config_mode(): |
| 37 | + self.exit_config_mode() |
| 38 | + except Exception: |
| 39 | + pass |
| 40 | + # Always try to send final command |
| 41 | + if self.session_log: |
| 42 | + self.session_log.fin = True |
| 43 | + self.write_channel(command + self.RETURN) |
| 44 | + |
| 45 | + def check_config_mode( |
| 46 | + self, |
| 47 | + check_string: str = ">configure", |
| 48 | + pattern: str = "#", |
| 49 | + force_regex: bool = False, |
| 50 | + ) -> bool: |
| 51 | + """Use equivalent enable method.""" |
| 52 | + return super().check_config_mode( |
| 53 | + check_string=check_string, pattern=pattern, force_regex=force_regex |
| 54 | + ) |
| 55 | + |
| 56 | + def config_mode( |
| 57 | + self, config_command: str = "configure", pattern: str = "", re_flags: int = 0 |
| 58 | + ) -> str: |
| 59 | + return super().config_mode( |
| 60 | + config_command=config_command, pattern=pattern, re_flags=re_flags |
| 61 | + ) |
| 62 | + |
| 63 | + def exit_config_mode(self, exit_config: str = "exit", pattern: str = "") -> str: |
| 64 | + return super().exit_config_mode(exit_config=exit_config) |
| 65 | + |
| 66 | + def save_config( |
| 67 | + self, cmd: str = "admin save", confirm: bool = False, confirm_response: str = "" |
| 68 | + ) -> str: |
| 69 | + return self._send_command_str( |
| 70 | + command_string=cmd, |
| 71 | + strip_prompt=False, |
| 72 | + strip_command=False, |
| 73 | + ) |
0 commit comments