"""Netmiko support for Extreme Ethernet Routing Switch."""
import time
from netmiko.cisco_base_connection import CiscoSSHConnection

# Extreme ERS presents Enter Ctrl-Y to begin.
CTRL_Y = "\x19"


class ExtremeErsSSH(CiscoSSHConnection):
    """Netmiko support for Extreme Ethernet Routing Switch."""

    def session_preparation(self) -> None:
        self._test_channel_read(pattern=r"[>#]")
        self.set_base_prompt()
        self.set_terminal_width()
        self.disable_paging()

    def special_login_handler(self, delay_factor: float = 1.0) -> None:
        """
        Extreme ERS presents the following as part of the login process:

        Enter Ctrl-Y to begin.
        """
        delay_factor = self.select_delay_factor(delay_factor)

        # Handle 'Enter Ctrl-Y to begin'
        output = ""
        i = 0
        while i <= 12:
            output = self.read_channel()
            if output:
                if "Ctrl-Y" in output:
                    self.write_channel(CTRL_Y)
                if "sername" in output:
                    assert isinstance(self.username, str)
                    self.write_channel(self.username + self.RETURN)
                elif "ssword" in output:
                    assert isinstance(self.password, str)
                    self.write_channel(self.password + self.RETURN)
                    break
                time.sleep(0.5 * delay_factor)
            else:
                self.write_channel(self.RETURN)
                time.sleep(1 * delay_factor)
            i += 1

    def save_config(
        self,
        cmd: str = "save config",
        confirm: bool = False,
        confirm_response: str = "",
    ) -> str:
        """Save Config"""
        return super().save_config(
            cmd=cmd, confirm=confirm, confirm_response=confirm_response
        )

@@ -59,19 +59,17 @@
 %0Aimport 
-tim
+r
 e%0Afrom n
@@ -122,16 +122,78 @@
 nnection
+%0Afrom netmiko.exceptions import NetmikoAuthenticationException
 %0A%0A# Extr
@@ -363,95 +363,180 @@
     
-def session_preparation(self) -%3E None:%0A        self._test_channel_read(pattern=r%22%5B%3E#%5D%22)
+prompt_pattern = r%22(?m:%5B%3E#%5D%5Cs*$)%22  # force re.Multiline%0A%0A    def session_preparation(self) -%3E None:%0A        # special_login_handler() will always ensure self.prompt_pattern
 %0A   
@@ -815,17 +815,17 @@
 to begin
-.
+%0A
 %0A       
@@ -829,280 +829,565 @@
     
-%22%22%22%0A        delay_factor = self.select_delay_factor(delay_factor)%0A%0A        # Handle 'Enter Ctrl-Y to begin'%0A        output = %22%22%0A        i = 0%0A        while i %3C= 12:%0A            output = self.read_channel()%0A            if output:%0A                if %22Ctrl-Y%22 in output:%0A    
+Older devices the Ctrl-Y is before SSH-login (not 100%25 sure of this).%0A%0A        Newer devices this is after SSH-login.%0A        %22%22%22%0A%0A        output = %22%22%0A        uname = %22sername%22%0A        password = %22ssword%22%0A        cntl_y = %22Ctrl-Y%22%0A        pattern = rf%22(?:%7Buname%7D%7C%7Bpassword%7D%7C%7Bcntl_y%7D%7C%7Bself.prompt_pattern%7D)%22%0A        while True:%0A            new_data = self.read_until_pattern(pattern=pattern, read_timeout=25.0)%0A            output += new_data%0A            if re.search(self.prompt_pattern, new_data):%0A                return%0A%0A            if cntl_y in new_data:%0A
     
@@ -1441,40 +1441,32 @@
     
-    if %22ser
+elif u
 name
-%22
  in 
-output:%0A    
+new_data:%0A
     
@@ -1523,36 +1523,32 @@
                 
-    
 self.write_chann
@@ -1595,35 +1595,33 @@
     
-    
 elif 
-%22
+pa
 ssword
-%22
  in 
-output
+new_data
 :%0A  
@@ -1626,36 +1626,32 @@
                 
-    
 assert isinstanc
@@ -1668,28 +1668,24 @@
 sword, str)%0A
-    
             
@@ -1752,21 +1752,13 @@
     
-        break
+else:
 %0A   
@@ -1774,168 +1774,172 @@
     
-time.sleep(0.5 * delay_factor)%0A            else:%0A                self.write_channel(self.RETURN)%0A                time.sleep(1 * delay_factor)%0A            i += 1
+msg = f%22%22%22%0AFailed to login to Extreme ERS Devices.%0A%0APattern not detected: %7Bpattern%7D%0Aoutput:%0A%0A%7Boutput%7D%0A%0A%22%22%22%0A                raise NetmikoAuthenticationException(msg)
 %0A%0A  

Fix for Extreme ERS login issues (#2814)