Repository: ktbyers/netmiko

Issue:
tplink "Pattern not detected: '[>#]' in output." in version 4.0.0 and above
Hello!

An error appears when connecting to tplink in version 4.0.0 and above, but in version 3.4.0 there is no error and the result is displayed

**Code:**
```
device = {
    "device_type": "tplink_jetstream",
    "host": host,
    "username": param["ssh_user"],
    "password": param["ssh_pass"],
}
with ConnectHandler(**device) as ssh:
    ssh.enable()
    output_before = ssh.send_command("sh system-info")
    print(output_before)
```

**Error:**
```
Pattern not detected: '[>#]' in output.

Things you might try to fix this:
1. Adjust the regex pattern to better identify the terminating string. Note, in
many situations the pattern is automatically based on the network device's prompt.
2. Increase the read_timeout to a larger value.

You can also look at the Netmiko session_log or debug log for more information.
```
**Debug:**
```
DEBUG:paramiko.transport:starting thread (client mode): 0x30757d50
DEBUG:paramiko.transport:Local version/idstring: SSH-2.0-paramiko_2.12.0
DEBUG:paramiko.transport:Remote version/idstring: SSH-1.99-IPSSH-6.6.0
INFO:paramiko.transport:Connected (version 1.99, client IPSSH-6.6.0)
DEBUG:paramiko.transport:=== Key exchange possibilities ===
DEBUG:paramiko.transport:kex algos: diffie-hellman-group1-sha1
DEBUG:paramiko.transport:server key: ssh-dss
DEBUG:paramiko.transport:client encrypt: aes128-cbc, aes192-cbc, aes256-cbc, blowfish-cbc, cast128-cbc, 3des-cbc
DEBUG:paramiko.transport:server encrypt: aes128-cbc, aes192-cbc, aes256-cbc, blowfish-cbc, cast128-cbc, 3des-cbc
DEBUG:paramiko.transport:client mac: hmac-sha1, hmac-sha1-96, hmac-md5, hmac-md5-96
DEBUG:paramiko.transport:server mac: hmac-sha1, hmac-sha1-96, hmac-md5, hmac-md5-96
DEBUG:paramiko.transport:client compress: none
DEBUG:paramiko.transport:server compress: none
DEBUG:paramiko.transport:client lang: <none>
DEBUG:paramiko.transport:server lang: <none>
DEBUG:paramiko.transport:kex follows: False
DEBUG:paramiko.transport:=== Key exchange agreements ===
DEBUG:paramiko.transport:Kex: diffie-hellman-group1-sha1
DEBUG:paramiko.transport:HostKey: ssh-dss
DEBUG:paramiko.transport:Cipher: aes128-cbc
DEBUG:paramiko.transport:MAC: hmac-sha1
DEBUG:paramiko.transport:Compression: none
DEBUG:paramiko.transport:=== End of kex handshake ===
DEBUG:paramiko.transport:kex engine KexGroup1 specified hash_algo <built-in function openssl_sha1>
DEBUG:paramiko.transport:Switch to new keys ...
DEBUG:paramiko.transport:Adding ssh-dss host key for 10.10.10.150: b'6e3ca009ab027terd8aa7e40d0516ef8'
DEBUG:paramiko.transport:userauth is OK
INFO:paramiko.transport:Authentication (password) successful!
DEBUG:paramiko.transport:[chan 0] Max packet in: 32768 bytes
DEBUG:paramiko.transport:[chan 0] Max packet out: 32768 bytes
DEBUG:paramiko.transport:Secsh channel 0 opened.
DEBUG:paramiko.transport:[chan 0] Sesch channel 0 request ok
DEBUG:paramiko.transport:[chan 0] Sesch channel 0 request ok
DEBUG:netmiko:write_channel: b'\r\n'
DEBUG:netmiko:read_channel: 

tp3428-test>


tp3428-test>
DEBUG:netmiko:Clear buffer detects data in the channel
DEBUG:netmiko:read_channel: 
DEBUG:netmiko:read_channel: 
DEBUG:netmiko:read_channel: 
...
EBUG:netmiko:read_channel: 
DEBUG:netmiko:write_channel: b'\r\n'
DEBUG:netmiko:read_channel: 
DEBUG:netmiko:read_channel: 


tp3428-test>
DEBUG:netmiko:read_channel: 
DEBUG:netmiko:read_channel: 
DEBUG:netmiko:read_channel: 
DEBUG:netmiko:read_channel: 
DEBUG:netmiko:read_channel: 
DEBUG:netmiko:read_channel: 
....
DEBUG:netmiko:read_channel: 
DEBUG:netmiko:read_channel: 
DEBUG:netmiko:read_channel: 
DEBUG:netmiko:read_channel: 
DEBUG:netmiko:read_channel: 
DEBUG:netmiko:read_channel: 
DEBUG:netmiko:write_channel: b'exit\r\n'
```
In version 3.4.0, the same code works without errors:
```
ThreadPoolExecutor-0_0 root INFO: ===> 18:53:05.075551 Connection: 10.10.10.150
 System Description   - JetStream 24-Port Gigabit L2+ Managed Switch with 4 10GE SFP+ Slots
 System Name          - tp3428-test
 System Location      - Hong Kong
 Contact Information  - www.tp-link.com
 Hardware Version     - TL-SG3428X 1.0
 Bootloader Version   - TP-LINK  BOOTUTIL(v1.0.0)
 System Time          - 2023-02-22 18:53:16
 Running Time         - 0 day - 5 hour - 20 min - 12 sec

```



Discussion:
Can you post the full exception stack trace so I can see where the error actually happens?
Thanks for the answer.
Here is the full exception stack trace:
```
(myenv) [user @ MBA test_netmiko]$ python netmiko_ssh.py 
Traceback (most recent call last):
  File "/Users/user/test_netmiko/netmiko_ssh.py", line 10, in <module>
    with ConnectHandler(**device) as ssh:
         ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/user/test_netmiko/myenv/lib/python3.11/site-packages/netmiko/ssh_dispatcher.py", line 365, in ConnectHandler
    return ConnectionClass(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/user/test_netmiko/myenv/lib/python3.11/site-packages/netmiko/tplink/tplink_jetstream.py", line 149, in __init__
    return super().__init__(**kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/user/test_netmiko/myenv/lib/python3.11/site-packages/netmiko/tplink/tplink_jetstream.py", line 21, in __init__
    return super().__init__(**kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/user/test_netmiko/myenv/lib/python3.11/site-packages/netmiko/base_connection.py", line 439, in __init__
    self._open()
  File "/Users/user/test_netmiko/myenv/lib/python3.11/site-packages/netmiko/base_connection.py", line 445, in _open
    self._try_session_preparation()
  File "/Users/user/test_netmiko/myenv/lib/python3.11/site-packages/netmiko/base_connection.py", line 904, in _try_session_preparation
    self.session_preparation()
  File "/Users/user/test_netmiko/myenv/lib/python3.11/site-packages/netmiko/tplink/tplink_jetstream.py", line 30, in session_preparation
    self._test_channel_read(pattern=r"[>#]")
  File "/Users/user/test_netmiko/myenv/lib/python3.11/site-packages/netmiko/base_connection.py", line 1144, in _test_channel_read
    return self.read_until_pattern(pattern=pattern, read_timeout=20)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/user/test_netmiko/myenv/lib/python3.11/site-packages/netmiko/base_connection.py", line 672, in read_until_pattern
    raise ReadTimeout(msg)
netmiko.exceptions.ReadTimeout: 

Pattern not detected: '[>#]' in output.

Things you might try to fix this:
1. Adjust the regex pattern to better identify the terminating string. Note, in
many situations the pattern is automatically based on the network device's prompt.
2. Increase the read_timeout to a larger value.

You can also look at the Netmiko session_log or debug log for more information.
```

Versions of installed packages:
```
(myenv) [user @ MBA test_netmiko]$ pip list
Package            Version
------------------ ---------
netmiko            4.1.2
paramiko           3.1.0
```
Sorry, didn't check the full stacktrace of the exception, on a quick glance - some problem in the `_test_channel_read` method of the `BaseConnection` class

Resolving patch:
diff --git a/netmiko/tplink/tplink_jetstream.py b/netmiko/tplink/tplink_jetstream.py
--- a/netmiko/tplink/tplink_jetstream.py
+++ b/netmiko/tplink/tplink_jetstream.py
@@ -21,15 +21,12 @@ def __init__(self, **kwargs: Any) -> None:
         return super().__init__(**kwargs)
 
     def session_preparation(self) -> None:
-        """
-        Prepare the session after the connection has been established.
-        """
         delay_factor = self.select_delay_factor(delay_factor=0)
         time.sleep(0.3 * delay_factor)
-        self.clear_buffer()
         self._test_channel_read(pattern=r"[>#]")
         self.set_base_prompt()
         self.enable()
+        self.set_base_prompt()
         self.disable_paging()
         # Clear the read buffer
         time.sleep(0.3 * self.global_delay_factor)


Test patch: