def asa_login(self): """ Handle ASA reaching privilege level 15 using login twb-dc-fw1> login Username: admin Raises NetmikoAuthenticationException, if we do not reach privilege level 15 after 10 loops. """ delay_factor = self.select_delay_factor(0) i = 1 max_attempts = 10 self.write_channel("login" + self.RETURN) output = self.read_until_pattern(pattern=r"login") while i <= max_attempts: time.sleep(0.5 * delay_factor) output = self.read_channel() if "sername" in output: self.write_channel(self.username + self.RETURN) elif "ssword" in output: self.write_channel(self.password + self.RETURN) elif "#" in output: return else: self.write_channel("login" + self.RETURN) i += 1 msg = "Unable to enter enable mode!" raise NetmikoAuthenticationException(msg)
def _test_channel_read(self, count=40, pattern=""): """Since Keymile NOS always returns True on paramiko.connect() we check the output for substring Login incorrect after connecting.""" output = super()._test_channel_read(count=count, pattern=pattern) pattern = r"Login incorrect" if re.search(pattern, output): self.paramiko_cleanup() msg = "Authentication failure: unable to connect" msg += f"{self.device_type} {self.host}:{self.port}" msg += self.RESPONSE_RETURN + "Login incorrect" raise NetmikoAuthenticationException(msg) else: return output
async def session_preparation(self): """ Prepare the session after the connection has been established Cisco WLC uses "config paging disable" to disable paging """ await asyncio.create_task(self._test_channel_read()) try: self.set_base_prompt() except ValueError: msg = f"Authentication failed: {self.host}" raise NetmikoAuthenticationException(msg) self.disable_paging(command="config paging disable") # Clear the read buffer await asyncio.sleep(0.3 * self.global_delay_factor) await asyncio.create_task(self.clear_buffer())
def telnet_login( self, pri_prompt_terminator=r"#\s*$", alt_prompt_terminator=r">\s*$", username_pattern=r"(Login|Username)", pwd_pattern=r"Password", delay_factor=1, max_loops=20, ): # set callback function to handle telnet options. self.remote_conn.set_option_negotiation_callback(self._process_option) delay_factor = self.select_delay_factor(delay_factor) time.sleep(1 * delay_factor) output = "" return_msg = "" i = 1 while i <= max_loops: try: output = self.read_channel() return_msg += output # Search for username pattern / send username if re.search(username_pattern, output, flags=re.I): self.write_channel(self.username + self.TELNET_RETURN) time.sleep(1 * delay_factor) output = self.read_channel() return_msg += output # Search for password pattern / send password if re.search(pwd_pattern, output, flags=re.I): self.write_channel(self.password + self.TELNET_RETURN) time.sleep(0.5 * delay_factor) output = self.read_channel() return_msg += output if re.search(pri_prompt_terminator, output, flags=re.M) or re.search( alt_prompt_terminator, output, flags=re.M): return return_msg # Check if proper data received if re.search(pri_prompt_terminator, output, flags=re.M) or re.search( alt_prompt_terminator, output, flags=re.M): return return_msg time.sleep(0.5 * delay_factor) i += 1 except EOFError: self.remote_conn.close() msg = f"Login failed: {self.host}" raise NetmikoAuthenticationException(msg) # Last try to see if we already logged in self.write_channel(self.TELNET_RETURN) time.sleep(0.5 * delay_factor) output = self.read_channel() return_msg += output if re.search(pri_prompt_terminator, output, flags=re.M) or re.search( alt_prompt_terminator, output, flags=re.M): return return_msg msg = f"Login failed: {self.host}" self.remote_conn.close() raise NetmikoAuthenticationException(msg)
def telnet_login( self, pri_prompt_terminator=r"#\s*$", alt_prompt_terminator=r">\s*$", username_pattern=r"(?:user:|username|login|user name)", pwd_pattern=r"assword", delay_factor=1, max_loops=20, ): """Telnet login. Can be username/password or just password.""" delay_factor = self.select_delay_factor(delay_factor) time.sleep(1 * delay_factor) output = "" return_msg = "" outer_loops = 3 inner_loops = int(max_loops / outer_loops) i = 1 for _ in range(outer_loops): while i <= inner_loops: try: output = self.read_channel() return_msg += output # Search for username pattern / send username if re.search(username_pattern, output, flags=re.I): # Sometimes username/password must be terminated with "\r" and not "\r\n" self.write_channel(self.username + "\r") time.sleep(1 * delay_factor) output = self.read_channel() return_msg += output # Search for password pattern / send password if re.search(pwd_pattern, output, flags=re.I): # Sometimes username/password must be terminated with "\r" and not "\r\n" self.write_channel(self.password + "\r") time.sleep(0.5 * delay_factor) output = self.read_channel() return_msg += output if re.search( pri_prompt_terminator, output, flags=re.M) or re.search( alt_prompt_terminator, output, flags=re.M): return return_msg # Support direct telnet through terminal server if re.search( r"initial configuration dialog\? \[yes/no\]: ", output): self.write_channel("no" + self.TELNET_RETURN) time.sleep(0.5 * delay_factor) count = 0 while count < 15: output = self.read_channel() return_msg += output if re.search(r"ress RETURN to get started", output): output = "" break time.sleep(2 * delay_factor) count += 1 # Check for device with no password configured if re.search(r"assword required, but none set", output): self.remote_conn.close() msg = "Login failed - Password required, but none set: {}".format( self.host) raise NetmikoAuthenticationException(msg) # Check if proper data received if re.search(pri_prompt_terminator, output, flags=re.M) or re.search( alt_prompt_terminator, output, flags=re.M): return return_msg i += 1 except EOFError: self.remote_conn.close() msg = f"Login failed: {self.host}" raise NetmikoAuthenticationException(msg) # Try sending an <enter> to restart the login process self.write_channel(self.TELNET_RETURN) time.sleep(0.5 * delay_factor) i = 1 # Last try to see if we already logged in self.write_channel(self.TELNET_RETURN) time.sleep(0.5 * delay_factor) output = self.read_channel() return_msg += output if re.search(pri_prompt_terminator, output, flags=re.M) or re.search( alt_prompt_terminator, output, flags=re.M): return return_msg self.remote_conn.close() msg = f"Login failed: {self.host}" raise NetmikoAuthenticationException(msg)
def telnet_login( self, pri_prompt_terminator=r"]\s*$", alt_prompt_terminator=r">\s*$", username_pattern=r"(?:user:|username|login|user name)", pwd_pattern=r"assword", delay_factor=1, max_loops=20, ): """Telnet login for Huawei Devices""" delay_factor = self.select_delay_factor(delay_factor) password_change_prompt = r"(Change now|Please choose 'YES' or 'NO').+" combined_pattern = r"({}|{}|{})".format(pri_prompt_terminator, alt_prompt_terminator, password_change_prompt) output = "" return_msg = "" i = 1 while i <= max_loops: try: output = self.read_channel() return_msg += output # Search for username pattern / send username if re.search(username_pattern, output, flags=re.I): self.write_channel(self.username + self.TELNET_RETURN) time.sleep(1 * delay_factor) output = self.read_channel() return_msg += output # Search for password pattern / send password if re.search(pwd_pattern, output, flags=re.I): self.write_channel(self.password + self.TELNET_RETURN) time.sleep(0.5 * delay_factor) output = self.read_channel() return_msg += output if re.search(pri_prompt_terminator, output, flags=re.M) or re.search( alt_prompt_terminator, output, flags=re.M): return return_msg # Search for password change prompt, send "N" if re.search(password_change_prompt, output): self.write_channel("N" + self.TELNET_RETURN) output = self.read_until_pattern(pattern=combined_pattern) return_msg += output # Check if proper data received if re.search(pri_prompt_terminator, output, flags=re.M) or re.search( alt_prompt_terminator, output, flags=re.M): return return_msg self.write_channel(self.TELNET_RETURN) time.sleep(0.5 * delay_factor) i += 1 except EOFError: self.remote_conn.close() msg = f"Login failed: {self.host}" raise NetmikoAuthenticationException(msg) # Last try to see if we already logged in self.write_channel(self.TELNET_RETURN) time.sleep(0.5 * delay_factor) output = self.read_channel() return_msg += output if re.search(pri_prompt_terminator, output, flags=re.M) or re.search( alt_prompt_terminator, output, flags=re.M): return return_msg self.remote_conn.close() msg = f"Login failed: {self.host}" raise NetmikoAuthenticationException(msg)