ktbyers / netmiko

Multi-vendor library to simplify Paramiko SSH connections to network devices
MIT License
3.55k stars 1.3k forks source link

Problem with Catalyst 1300 - random detect pattern and noting more. #3474

Open iniagolov opened 1 month ago

iniagolov commented 1 month ago

Screenshot_20240808_210625-1300

Netmiko version

netmiko==4.4.0

Netmiko device_type (if relevant to the issue)

cisco_xr

======

Code

from netmiko import ConnectHandler
import logging
logging.basicConfig(filename="test.log", level=logging.DEBUG)
logger = logging.getLogger("netmiko")
net_connect = ConnectHandler(host="192.168.10.1", username="username", password="password", device_type="cisco_xr", secret="", port=22,)

Error Traceback

cat test.log
DEBUG:paramiko.transport:starting thread (client mode): 0xf6409890
DEBUG:paramiko.transport:Local version/idstring: SSH-2.0-paramiko_3.4.0
DEBUG:paramiko.transport:Remote version/idstring: SSH-2.0-OpenSSH_7.3p1.RL
INFO:paramiko.transport:Connected (version 2.0, client OpenSSH_7.3p1.RL)
DEBUG:paramiko.transport:=== Key exchange possibilities ===
DEBUG:paramiko.transport:kex algos: diffie-hellman-group16-sha512, diffie-hellman-group14-sha1
DEBUG:paramiko.transport:server key: rsa-sha2-512, rsa-sha2-256, ssh-rsa, ssh-dss
DEBUG:paramiko.transport:client encrypt: aes128-ctr, aes192-ctr, aes256-ctr, chacha20-poly1305@openssh.com, aes128-gcm@openssh.com, aes256-gcm@openssh.com
DEBUG:paramiko.transport:server encrypt: aes128-ctr, aes192-ctr, aes256-ctr, chacha20-poly1305@openssh.com, aes128-gcm@openssh.com, aes256-gcm@openssh.com
DEBUG:paramiko.transport:client mac: hmac-sha2-256, hmac-sha2-512, hmac-sha1
DEBUG:paramiko.transport:server mac: hmac-sha2-256, hmac-sha2-512, hmac-sha1
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-group16-sha512
DEBUG:paramiko.transport:HostKey: rsa-sha2-512
DEBUG:paramiko.transport:Cipher: aes128-ctr
DEBUG:paramiko.transport:MAC: hmac-sha2-256
DEBUG:paramiko.transport:Compression: none
DEBUG:paramiko.transport:=== End of kex handshake ===
DEBUG:paramiko.transport:kex engine KexGroup16SHA512 specified hash_algo <built-in function openssl_sha512>
DEBUG:paramiko.transport:Switch to new keys ...
DEBUG:paramiko.transport:Adding ssh-rsa host key for 192.168.10.1: b'4ff6f5c57f4c093a942519d34b17ae3d'
DEBUG:paramiko.transport:Got EXT_INFO: {'server-sig-algs': b'rsa-sha2-256,rsa-sha2-512'}
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'\n'
DEBUG:netmiko:read_channel: 

ciskoS1#
DEBUG:netmiko:Pattern found: ([>#]) 

ciskoS1#
DEBUG:netmiko:write_channel: b'terminal width 511\n'
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: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: 

ciskoS1#
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: termi
DEBUG:netmiko:read_channel: nal widt
DEBUG:netmiko:read_channel: h 511

ciskoS1#
DEBUG:netmiko:Pattern found: (terminal width 511) 

ciskoS1#terminal width 511
DEBUG:netmiko:In disable_paging
DEBUG:netmiko:Command: terminal length 0

DEBUG:netmiko:write_channel: b'terminal length 0\n'
DEBUG:netmiko:read_channel: 
DEBUG:netmiko:read_channel: 
DEBUG:netmiko:read_channel: 
DEBUG:netmiko:read_channel: t
DEBUG:netmiko:read_channel: 
DEBUG:netmiko:read_channel: erminal length 0

% Unrecognized command

DEBUG:netmiko:Pattern found: (terminal\ length\ 0) 

ciskoS1#terminal length 0
DEBUG:netmiko:

ciskoS1#terminal length 0
DEBUG:netmiko:Exiting disable_paging
DEBUG:netmiko:read_channel: ciskoS1#
DEBUG:netmiko:Pattern found: ([>#]) 

% Unrecognized command
ciskoS1#
DEBUG:netmiko:read_channel: 
DEBUG:netmiko:write_channel: b'\n'
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: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: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: 

ciskoS1#
DEBUG:netmiko:
Parenthesis found in pattern.

pattern: (\#|>)

This can be problemtic when used in read_until_pattern().

You should ensure that you use either non-capture groups i.e. '(?:' or that the
parenthesis completely wrap the pattern '(pattern)'
DEBUG:netmiko:Pattern found: (\#|>) 

ciskoS1#
DEBUG:netmiko:read_channel: 
DEBUG:netmiko:[find_prompt()]: prompt is ciskoS1#

======

Code

>>> net_connect.send_command("show ver")
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/var/www/networkmanager.tss.bg/.venv/lib/python3.11/site-packages/netmiko/base_connection.py", line 111, in wrapper_decorator
    return_val = func(self, *args, **kwargs)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/var/www/networkmanager.tss.bg/.venv/lib/python3.11/site-packages/netmiko/utilities.py", line 596, in wrapper_decorator
    return func(self, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/var/www/networkmanager.tss.bg/.venv/lib/python3.11/site-packages/netmiko/base_connection.py", line 1826, in send_command
    raise ReadTimeout(msg)
netmiko.exceptions.ReadTimeout: 
Pattern not detected: '\x1b\\[KciskoS1\\#' in output.

Things you might try to fix this:
1. Explicitly set your pattern using the expect_string argument.
2. Increase the read_timeout to a larger value.

You can also look at the Netmiko session_log or debug log for more information

Error Traceback

DEBUG:netmiko:read_channel: 
DEBUG:netmiko:write_channel: b'\n'
DEBUG:netmiko:read_channel: 
DEBUG:netmiko:write_channel: b'\n'
DEBUG:netmiko:read_channel: 
DEBUG:netmiko:write_channel: b'\n'
DEBUG:netmiko:read_channel: 
DEBUG:netmiko:write_channel: b'\n'
DEBUG:netmiko:read_channel: 

ciskoS1#
DEBUG:netmiko:read_channel: 
DEBUG:netmiko:[find_prompt()]: prompt is ciskoS1#
DEBUG:netmiko:write_channel: b'show ver\n'
DEBUG:netmiko:read_channel: 
DEBUG:netmiko:read_channel: 
DEBUG:netmiko:read_channel: 

DEBUG:netmiko:read_channel: 
DEBUG:netmiko:read_channel: 

ciskoS1#
DEBUG:netmiko:read_channel: 
DEBUG:netmiko:read_channel: 

DEBUG:netmiko:read_channel: 
DEBUG:netmiko:read_channel: 

ciskoS1#
DEBUG:netmiko:read_channel: 
DEBUG:netmiko:read_channel: 

DEBUG:netmiko:read_channel: 
DEBUG:netmiko:read_channel: 

ciskoS1#
DEBUG:netmiko:read_channel: 
DEBUG:netmiko:read_channel: s
DEBUG:netmiko:read_channel: 
DEBUG:netmiko:read_channel: 
DEBUG:netmiko:read_channel: 
DEBUG:netmiko:read_channel: how ver

Active-image: flash://system/images/image_c1300_4.1.3.36_official_key.bin

DEBUG:netmiko:Pattern found: (show\ ver) 

ciskoS1#

ciskoS1#

ciskoS1#show ver
DEBUG:netmiko:read_channel: 
DEBUG:netmiko:read_channel: 
DEBUG:netmiko:read_channel:   Version: 4.1.3.36

DEBUG:netmiko:read_channel: 
DEBUG:netmiko:read_channel:   MD5 Digest: 90803a985c9110cef9aa4d576206b629
  Date: 19-May-2024
  Time: 08:17:26
Inactive-image: flash://system/images/_image_c1300_4.1.3.36_official_key.bin
  Version: 4.1.3.36
  MD5 Digest: 90803a985c9110cef9aa4d576206b629
  Date: 19-May-2024
  Time: 08:17:26
ciskoS1#
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: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:read_channel: 
DEBUG:netmiko:read_channel:

======

Litle info from switch

ciskoS1#show version 
Active-image: flash://system/images/image_c1300_4.1.3.36_official_key.bin
  Version: 4.1.3.36
  MD5 Digest: 90803a985c9110cef9aa4d576206b629
  Date: 19-May-2024
  Time: 08:17:26
Inactive-image: flash://system/images/_image_c1300_4.1.3.36_official_key.bin
  Version: 4.1.3.36
  MD5 Digest: 90803a985c9110cef9aa4d576206b629
  Date: 19-May-2024
  Time: 08:17:26
ciskoS1#
ktbyers commented 1 month ago

That is not "ios_xr" i.e. your device_type is wrong.

Also disabling of output paging is not working and there are ANSI escape codes are in the output?

It is a bit unclear what the actual operating system of the catalyst 1300 is? You can try "cisco_ios", but you will need to figure out why terminal length 0 is not working.

Also you probably need to disable the output paging, from looking at the CLI manual for Cat 1300, it looks like `Example The following example dumps all output immediately after entering a show command.

terminal datadump

Also need to set:

net_connect.ansi_escape_codes = True

So your code should become:

from netmiko import ConnectHandler
import logging
logging.basicConfig(filename="test.log", level=logging.DEBUG)
logger = logging.getLogger("netmiko")
net_connect = ConnectHandler(
    host="192.168.10.1", 
    username="username", 
    password="password", 
    device_type="cisco_ios",   # Note change
    secret="", port=22,
)

net_connect.send_command("terminal datadump", expect_string=r"#")
net_connect.ansi_escape_codes = True

# Then try your "show" command using the "send_command" method.

Let me know if this works and we can build a driver for this platform.

Reference to notes on Operating System for Catalyst 1200 (and assume 1300):

https://www.reddit.com/r/Cisco/comments/185xmg0/os_used_in_the_catalyst_1200/

iniagolov commented 1 month ago

Of course I would like you to create a driver for this platform, just tell me what I can help with.

Code:

from netmiko import ConnectHandler
import logging
logging.basicConfig(filename="test.log", level=logging.DEBUG)
logger = logging.getLogger("netmiko")
net_connect = ConnectHandler(host="192.168.10.1", username="username", password="password", device_type="cisco_ios", secret="", port=22, )
net_connect.send_command("terminal datadump", expect_string=r"#")
net_connect.ansi_escape_codes = True
net_connect.send_command("show ver")
net_connect.send_command("show interface status")
net_connect.disconnect()

Result

>>> net_connect.disconnect()
>>> net_connect = ConnectHandler(host="192.168.10.1", username="networkmanager", password="N@Man390erp", device_type="cisco_ios", secret="", port=22, )
>>> net_connect.send_command("terminal datadump", expect_string=r"#")
'\nciscoS1#'
>>> net_connect.ansi_escape_codes = True
>>> net_connect.send_command("show ver")
'\nActive-image: flash://system/images/image_c1300_4.1.3.36_official_key.bin\n  Version: 4.1.3.36\n  MD5 Digest: 90803a985c9110cef9aa4d576206b629\n  Date: 19-May-2024\n  Time: 08:17:26\nInactive-image: flash://system/images/_image_c1300_4.1.3.36_official_key.bin\n  Version: 4.1.3.36\n  MD5 Digest: 90803a985c9110cef9aa4d576206b629\n  Date: 19-May-2024\n  Time: 08:17:26\nciscoS1#'
>>> 
>>> net_connect.send_command("show interface status")
'\n                                             Flow Link          Back   Mdix\nPort     Type         Duplex  Speed Neg      ctrl State       Pressure Mode\n-------- ------------ ------  ----- -------- ---- ----------- -------- -------\ngi1/0/1  1G-Copper    Full    1000  Enabled  Off  Up          Disabled Off    \ngi1/0/2  1G-Copper      --      --     --     --  Down           --     --    \ngi1/0/3  1G-Copper      --      --     --     --  Down           --     --    \ngi1/0/4  1G-Copper      --      --     --     --  Down           --     --    \ngi1/0/5  1G-Copper      --      --     --     --  Down           --     --    \ngi1/0/6  1G-Copper      --      --     --     --  Down           --     --    \ngi1/0/7  1G-Copper      --      --     --     --  Down           --     --    \ngi1/0/8  1G-Copper      --      --     --     --  Down           --     --    \ngi1/0/9  1G-Copper      --      --     --     --  Down           --     --    \ngi1/0/10 1G-Copper      --      --     --     --  Down           --     --    \ngi1/0/11 1G-Copper      --      --     --     --  Down           --     --    \ngi1/0/12 1G-Copper      --      --     --     --  Down           --     --    \ngi1/0/13 1G-Copper    Full    1000  Enabled  Off  Up          Disabled Off    \ngi1/0/14 1G-Copper    Full    1000  Enabled  Off  Up          Disabled Off    \ngi1/0/15 1G-Copper      --      --     --     --  Down           --     --    \ngi1/0/16 1G-Copper    Full    1000  Enabled  Off  Up          Disabled Off    \ngi1/0/17 1G-Copper      --      --     --     --  Down           --     --    \ngi1/0/18 1G-Copper      --      --     --     --  Down           --     --    \ngi1/0/19 1G-Copper      --      --     --     --  Down           --     --    \ngi1/0/20 1G-Copper      --      --     --     --  Down           --     --    \ngi1/0/21 1G-Copper      --      --     --     --  Down           --     --    \ngi1/0/22 1G-Copper      --      --     --     --  Down           --     --    \ngi1/0/23 1G-Copper    Full    1000  Enabled  Off  Up          Disabled On     \ngi1/0/24 1G-Copper    Full    1000  Enabled  Off  Up          Disabled Off    \nte1/0/1  10G-Fiber    Full    10000 Disabled Off  Up          Disabled Off    \nte1/0/2  10G-Fiber    Full    10000 Disabled Off  Up          Disabled Off    \ngi2/0/1  1G-Copper    Full    1000  Enabled  Off  Up          Disabled On     \ngi2/0/2  1G-Copper      --      --     --     --  Down           --     --    \ngi2/0/3  1G-Copper      --      --     --     --  Down           --     --    \ngi2/0/4  1G-Copper      --      --     --     --  Down           --     --    \ngi2/0/5  1G-Copper      --      --     --     --  Down           --     --    \ngi2/0/6  1G-Copper      --      --     --     --  Down           --     --    \ngi2/0/7  1G-Copper      --      --     --     --  Down           --     --    \ngi2/0/8  1G-Copper      --      --     --     --  Down           --     --    \ngi2/0/9  1G-Copper      --      --     --     --  Down           --     --    \ngi2/0/10 1G-Copper      --      --     --     --  Down           --     --    \ngi2/0/11 1G-Copper      --      --     --     --  Down           --     --    \ngi2/0/12 1G-Copper    Full    1000  Enabled  Off  Up          Disabled Off    \ngi2/0/13 1G-Copper      --      --     --     --  Down           --     --    \ngi2/0/14 1G-Copper      --      --     --     --  Down           --     --    \ngi2/0/15 1G-Copper      --      --     --     --  Down           --     --    \ngi2/0/16 1G-Copper    Full    1000  Enabled  Off  Up          Disabled Off    \ngi2/0/17 1G-Copper      --      --     --     --  Down           --     --    \ngi2/0/18 1G-Copper    Full    1000  Enabled  Off  Up          Disabled Off    \ngi2/0/19 1G-Copper      --      --     --     --  Down           --     --    \ngi2/0/20 1G-Copper      --      --     --     --  Down           --     --    \ngi2/0/21 1G-Copper      --      --     --     --  Down           --     --    \ngi2/0/22 1G-Copper      --      --     --     --  Down           --     --    \ngi2/0/23 1G-Copper    Full    1000  Enabled  Off  Up          Disabled Off    \ngi2/0/24 1G-Copper      --      --     --     --  Down           --     --    \nte2/0/1  10G-Fiber    Full    10000 Disabled Off  Up          Disabled Off    \nte2/0/2  10G-Fiber    Full    10000 Disabled Off  Up          Disabled Off    \n\n                                          Flow    Link        \nCh       Type    Duplex  Speed  Neg      control  State       \n-------- ------- ------  -----  -------- -------  ----------- \nPo1      10G     Full    10000  Enabled  Off      Up          \nPo2      10G     Full    10000  Enabled  Off      Up          \nPo3      1G      Full    1000   Enabled  Off      Up          \nPo4         --     --      --      --       --    Not Present \nPo5         --     --      --      --       --    Not Present \nPo6         --     --      --      --       --    Not Present \nPo7         --     --      --      --       --    Not Present \nPo8         --     --      --      --       --    Not Present \nciscoS1#'
>>> 
>>> net_connect.disconnect()
>>> 

Debug result test.log

ktbyers commented 1 month ago

@iniagolov Did that all work properly from an end-user perspective?

iniagolov commented 1 month ago
>>> net_connect.send_command("show interface status")
'\n                                             Flow Link          Back   Mdix\nPort     Type         Duplex  Speed Neg      ctrl State       Pressure Mode\n-------- ------------ ------  ----- -------- ---- ----------- -------- -------\ngi1/0/1  1G-Copper    Full    1000  Enabled  Off  Up          Disabled Off    \ngi1/0/2  1G-Copper      --      --     --     --  Down           --     --    \ngi1/0/3  1G-Copper      --      --     --     --  Down           --     --    \ngi1/0/4  1G-Copper      --      --     --     --  Down           --     --    \ngi1/0/5  1G-Copper      --      --     --     --  Down           --     --    \ngi1/0/6  1G-Copper      --      --     --     --  Down           --     --    \ngi1/0/7  1G-Copper      --      --     --     --  Down           --     --    \ngi1/0/8  1G-Copper      --      --     --     --  Down           --     --    \ngi1/0/9  1G-Copper      --      --     --     --  Down           --     --    \ngi1/0/10 1G-Copper      --      --     --     --  Down           --     --    \ngi1/0/11 1G-Copper      --      --     --     --  Down           --     --    \ngi1/0/12 1G-Copper      --      --     --     --  Down           --     --    \ngi1/0/13 1G-Copper    Full    1000  Enabled  Off  Up          Disabled Off    \ngi1/0/14 1G-Copper    Full    1000  Enabled  Off  Up          Disabled Off    \ngi1/0/15 1G-Copper      --      --     --     --  Down           --     --    \ngi1/0/16 1G-Copper    Full    1000  Enabled  Off  Up          Disabled Off    \ngi1/0/17 1G-Copper      --      --     --     --  Down           --     --    \ngi1/0/18 1G-Copper      --      --     --     --  Down           --     --    \ngi1/0/19 1G-Copper      --      --     --     --  Down           --     --    \ngi1/0/20 1G-Copper      --      --     --     --  Down           --     --    \ngi1/0/21 1G-Copper      --      --     --     --  Down           --     --    \ngi1/0/22 1G-Copper      --      --     --     --  Down           --     --    \ngi1/0/23 1G-Copper    Full    1000  Enabled  Off  Up          Disabled On     \ngi1/0/24 1G-Copper    Full    1000  Enabled  Off  Up          Disabled Off    \nte1/0/1  10G-Fiber    Full    10000 Disabled Off  Up          Disabled Off    \nte1/0/2  10G-Fiber    Full    10000 Disabled Off  Up          Disabled Off    \ngi2/0/1  1G-Copper    Full    1000  Enabled  Off  Up          Disabled On     \ngi2/0/2  1G-Copper      --      --     --     --  Down           --     --    \ngi2/0/3  1G-Copper      --      --     --     --  Down           --     --    \ngi2/0/4  1G-Copper      --      --     --     --  Down           --     --    \ngi2/0/5  1G-Copper      --      --     --     --  Down           --     --    \ngi2/0/6  1G-Copper      --      --     --     --  Down           --     --    \ngi2/0/7  1G-Copper      --      --     --     --  Down           --     --    \ngi2/0/8  1G-Copper      --      --     --     --  Down           --     --    \ngi2/0/9  1G-Copper      --      --     --     --  Down           --     --    \ngi2/0/10 1G-Copper      --      --     --     --  Down           --     --    \ngi2/0/11 1G-Copper      --      --     --     --  Down           --     --    \ngi2/0/12 1G-Copper    Full    1000  Enabled  Off  Up          Disabled Off    \ngi2/0/13 1G-Copper      --      --     --     --  Down           --     --    \ngi2/0/14 1G-Copper      --      --     --     --  Down           --     --    \ngi2/0/15 1G-Copper      --      --     --     --  Down           --     --    \ngi2/0/16 1G-Copper    Full    1000  Enabled  Off  Up          Disabled Off    \ngi2/0/17 1G-Copper      --      --     --     --  Down           --     --    \ngi2/0/18 1G-Copper    Full    1000  Enabled  Off  Up          Disabled Off    \ngi2/0/19 1G-Copper      --      --     --     --  Down           --     --    \ngi2/0/20 1G-Copper      --      --     --     --  Down           --     --    \ngi2/0/21 1G-Copper      --      --     --     --  Down           --     --    \ngi2/0/22 1G-Copper      --      --     --     --  Down           --     --    \ngi2/0/23 1G-Copper    Full    1000  Enabled  Off  Up          Disabled Off    \ngi2/0/24 1G-Copper      --      --     --     --  Down           --     --    \nte2/0/1  10G-Fiber    Full    10000 Disabled Off  Up          Disabled Off    \nte2/0/2  10G-Fiber    Full    10000 Disabled Off  Up          Disabled Off    \n\n                                          Flow    Link        \nCh       Type    Duplex  Speed  Neg      control  State       \n-------- ------- ------  -----  -------- -------  ----------- \nPo1      10G     Full    10000  Enabled  Off      Up          \nPo2      10G     Full    10000  Enabled  Off      Up          \nPo3      1G      Full    1000   Enabled  Off      Up          \nPo4         --     --      --      --       --    Not Present \nPo5         --     --      --      --       --    Not Present \nPo6         --     --      --      --       --    Not Present \nPo7         --     --      --      --       --    Not Present \nPo8         --     --      --      --       --    Not Present \nciscoS1#'
>>> 

work

but this not work!

>>> net_connect.send_command("show interface status", use_textfsm=True)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/var/www/networkmanager.tss.bg/.venv/lib/python3.11/site-packages/netmiko/base_connection.py", line 111, in wrapper_decorator
    return_val = func(self, *args, **kwargs)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/var/www/networkmanager.tss.bg/.venv/lib/python3.11/site-packages/netmiko/utilities.py", line 596, in wrapper_decorator
    return func(self, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/var/www/networkmanager.tss.bg/.venv/lib/python3.11/site-packages/netmiko/base_connection.py", line 1834, in send_command
    return_val = structured_data_converter(
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/var/www/networkmanager.tss.bg/.venv/lib/python3.11/site-packages/netmiko/utilities.py", line 560, in structured_data_converter
    structured_output_tfsm = get_structured_data_textfsm(
                             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/var/www/networkmanager.tss.bg/.venv/lib/python3.11/site-packages/netmiko/utilities.py", line 380, in get_structured_data_textfsm
    output = _textfsm_parse(textfsm_obj, raw_output, attrs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/var/www/networkmanager.tss.bg/.venv/lib/python3.11/site-packages/netmiko/utilities.py", line 344, in _textfsm_parse
    tfsm_parse(raw_output, attrs)
  File "/var/www/networkmanager.tss.bg/.venv/lib/python3.11/site-packages/textfsm/clitable.py", line 282, in ParseCmd
    self.table = self._ParseCmdItem(self.raw, template_file=template_files[0])
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/var/www/networkmanager.tss.bg/.venv/lib/python3.11/site-packages/textfsm/clitable.py", line 315, in _ParseCmdItem
    for record in fsm.ParseText(cmd_input):
                  ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/var/www/networkmanager.tss.bg/.venv/lib/python3.11/site-packages/textfsm/parser.py", line 895, in ParseText
    self._CheckLine(line)
  File "/var/www/networkmanager.tss.bg/.venv/lib/python3.11/site-packages/textfsm/parser.py", line 944, in _CheckLine
    if self._Operations(rule, line):
       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/var/www/networkmanager.tss.bg/.venv/lib/python3.11/site-packages/textfsm/parser.py", line 1024, in _Operations
    raise TextFSMError('State Error raised. Rule Line: %s. Input Line: %s'
textfsm.parser.TextFSMError: State Error raised. Rule Line: 17. Input Line:                                              Flow Link          Back   Mdix
>>> 

Debug

DEBUG:netmiko:read_channel: 08-Aug-2024 23:11:35 %COPY-I-FILECPY: Files Copy - source URL running-config destination URL flash://system/configuration/startup-config
08-Aug-2024 23:11:40 %COPY-N-TRAP: The copy operation was completed successfully

DEBUG:netmiko:Clear buffer detects data in the channel
DEBUG:netmiko:read_channel: 
DEBUG:netmiko:write_channel: b'\n'
DEBUG:netmiko:read_channel: 
DEBUG:netmiko:write_channel: b'\n'
DEBUG:netmiko:read_channel: 
DEBUG:netmiko:write_channel: b'\n'
DEBUG:netmiko:read_channel: 
DEBUG:netmiko:write_channel: b'\n'
DEBUG:netmiko:read_channel: 

ciscoS1#
DEBUG:netmiko:read_channel: 
DEBUG:netmiko:[find_prompt()]: prompt is ciscoS1#
DEBUG:netmiko:write_channel: b'show interface status\n'
DEBUG:netmiko:read_channel: 
DEBUG:netmiko:read_channel: 

ciscoS1#
DEBUG:netmiko:read_channel: 
DEBUG:netmiko:read_channel: 

ciscoS1#
DEBUG:netmiko:read_channel: 
DEBUG:netmiko:read_channel: 

ciscoS1#
DEBUG:netmiko:read_channel: 
DEBUG:netmiko:read_channel: show inter
DEBUG:netmiko:read_channel: face status
DEBUG:netmiko:Pattern found: (show\ interface\ status) 

ciscoS1#

ciscoS1#

ciscoS1#show interface status
DEBUG:netmiko:read_channel: 
DEBUG:netmiko:read_channel: 

                                             Flow Link          Back   Mdix
DEBUG:netmiko:read_channel: 
DEBUG:netmiko:read_channel: 
Port     Type         Duplex  Speed Neg      ctrl State       Pressure Mode
-------- ------------ ------  ----- -------- ---- ----------- -------- -------
gi1/0/1  1G-Copper    Full    1000  Enabled  Off  Up          Disabled Off    
gi1/0/2  1G-Copper      --      --     --     --  Down           --     --    
gi1/0/3  1G-Copper      --      --     --     --  Down           --     --    

DEBUG:netmiko:read_channel: 
DEBUG:netmiko:read_channel: 
DEBUG:netmiko:read_channel: 
DEBUG:netmiko:read_channel: 
DEBUG:netmiko:read_channel: gi1/0/4  1G-Copper      --      --     --     --  Down           --     --    
gi1/0/5  1G-Copper      --      --     --     --  Down           --     --    
gi1/0/6  1G-Copper      --      --     --     --  Down           --     --    
gi1/0/7  1G-Copper      --      --     --     --  Down           --     --    
gi1/0/8  1G-Copper      --      --     --     --  Down           --     --    
gi1/0/9  1G-Copper      --      --     --     --  Down           --     --    
gi1/0/10 1G-Copper      --      --     --     --  Down           --     --    
gi1/0/11 1G-Copper      --      --     --     --  Down           --     --    
gi1/0/12 1G-Copper      --      --     --     --  Down           --     --    
gi1/0/13 1G-Copper    Full    1000  Enabled  Off  Up          Disabled Off    
DEBUG:netmiko:read_channel: 
gi1/0/14 1G-Copper    Full    1000  Enabled  Off  Up          Disabled Off    
DEBUG:netmiko:read_channel: 
gi1/0/15 1G-Copper      --      --     --     --  Down           --     --    
gi1/0/16 1G-Copper    Full    1000  Enabled  Off  Up          Disabled Off    
DEBUG:netmiko:read_channel: 
gi1/0/17 1G-Copper      --      --     --     --  Down           --     --    

DEBUG:netmiko:read_channel: gi1/0/18 1G-Copper      --      --     --     --  Down           --     --    
gi1/0/19 1G-Copper      --      --     --     --  Down           --     --    
DEBUG:netmiko:read_channel: 
gi1/0/20 1G-Copper      --      --     --     --  Down           --     --    
gi1/0/21 1G-Copper      --      --     --     --  Down           --     --    
DEBUG:netmiko:read_channel: 
gi1/0/22 1G-Copper      --      --     --     --  Down           --     --    
DEBUG:netmiko:read_channel: 
DEBUG:netmiko:read_channel: 
DEBUG:netmiko:read_channel: 
gi1/0/23 1G-Copper    Full    1000  Enabled  Off  Up          Disabled On     
gi1/0/24 1G-Copper    Full    1000  Enabled  Off  Up          Disabled Off    
te1/0/1  10G-Fiber    Full    10000 Disabled Off  Up          Disabled Off    

DEBUG:netmiko:read_channel: te1/0/2  10G-Fiber    Full    10000 Disabled Off  Up          Disabled Off    
gi2/0/1  1G-Copper    Full    1000  Enabled  Off  Up          Disabled On     

DEBUG:netmiko:read_channel: gi2/0/2  1G-Copper      --      --     --     --  Down           --     --    

DEBUG:netmiko:read_channel: gi2/0/3  1G-Copper      --      --     --     --  Down           --     --    

DEBUG:netmiko:read_channel: gi2/0/4  1G-Copper      --      --     --     --  Down           --     --    
gi2/0/5  1G-Copper      --      --     --     --  Down           --     --    

DEBUG:netmiko:read_channel: 
DEBUG:netmiko:read_channel: 
DEBUG:netmiko:read_channel: gi2/0/6  1G-Copper      --      --     --     --  Down           --     --    
gi2/0/7  1G-Copper      --      --     --     --  Down           --     --    

DEBUG:netmiko:read_channel: 
DEBUG:netmiko:read_channel: 
DEBUG:netmiko:read_channel: 
DEBUG:netmiko:read_channel: gi2/0/8  1G-Copper      --      --     --     --  Down           --     --    
gi2/0/9  1G-Copper      --      --     --     --  Down           --     --    
gi2/0/10 1G-Copper      --      --     --     --  Down           --     --    
gi2/0/11 1G-Copper      --      --     --     --  Down           --     --    
gi2/0/12 1G-Copper    Full    1000  Enabled  Off  Up          Disabled Off    
gi2/0/13 1G-Copper      --      --     --     --  Down           --     --    
gi2/0/14 1G-Copper      --      --     --     --  Down           --     --    

DEBUG:netmiko:read_channel: 
DEBUG:netmiko:read_channel: 
DEBUG:netmiko:read_channel: gi2/0/15 1G-Copper      --      --     --     --  Down           --     --    
gi2/0/16 1G-Copper    Full    1000  Enabled  Off  Up          Disabled Off    
gi2/0/17 1G-Copper      --      --     --     --  Down           --     --    
gi2/0/18 1G-Copper    Full    1000  Enabled  Off  Up          Disabled Off    
DEBUG:netmiko:read_channel: 
gi2/0/19 1G-Copper      --      --     --     --  Down           --     --    

DEBUG:netmiko:read_channel: gi2/0/20 1G-Copper      --      --     --     --  Down           --     --    
gi2/0/21 1G-Copper      --      --     --     --  Down           --     --    
DEBUG:netmiko:read_channel: 
gi2/0/22 1G-Copper      --      --     --     --  Down           --     --    
DEBUG:netmiko:read_channel: 
gi2/0/23 1G-Copper    Full    1000  Enabled  Off  Up          Disabled Off    
DEBUG:netmiko:read_channel: 
gi2/0/24 1G-Copper      --      --     --     --  Down           --     --    

DEBUG:netmiko:read_channel: te2/0/1  10G-Fiber    Full    10000 Disabled Off  Up          Disabled Off    

DEBUG:netmiko:read_channel: te2/0/2  10G-Fiber    Full    10000 Disabled Off  Up          Disabled Off    

DEBUG:netmiko:read_channel: 
                                          Flow    Link        
Ch       Type    Duplex  Speed  Neg      control  State       
-------- ------- ------  -----  -------- -------  ----------- 
DEBUG:netmiko:read_channel: 

DEBUG:netmiko:read_channel: Po1      10G     Full    10000  Enabled  Off      Up          
DEBUG:netmiko:read_channel: 
Po2      10G     Full    10000  Enabled  Off      Up          
DEBUG:netmiko:read_channel: 
Po3      1G      Full    1000   Enabled  Off      Up          
DEBUG:netmiko:read_channel: 
DEBUG:netmiko:read_channel: 
DEBUG:netmiko:read_channel: 
Po4         --     --      --      --       --    Not Present 
Po5         --     --      --      --       --    Not Present 
Po6         --     --      --      --       --    Not Present 
Po7         --     --      --      --       --    Not Present 
Po8         --     --      --      --       --    Not Present 
DEBUG:netmiko:read_channel: 
DEBUG:netmiko:read_channel: 
DEBUG:netmiko:read_channel: 
DEBUG:netmiko:read_channel: 
ciscoS1#
iniagolov commented 1 month ago

That doesn't work either

from netmiko import ConnectHandler
import logging
logging.basicConfig(filename="test.log", level=logging.DEBUG)
logger = logging.getLogger("netmiko")
net_connect = ConnectHandler(host="192.168.10.1", username="networkmanager", password="N@Man390erp", device_type="cisco_ios", secret="", port=22, )
net_connect.send_command("terminal datadump", expect_string=r"#")
net_connect.ansi_escape_codes = True

net_connect.send_config_set('int gi1/0/3,no shutdown')

net_connect.disconnect()

Result

>>> net_connect.send_config_set('int gi1/0/3,no shutdown')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/var/www/networkmanager.tss.bg/.venv/lib/python3.11/site-packages/netmiko/base_connection.py", line 111, in wrapper_decorator
    return_val = func(self, *args, **kwargs)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/var/www/networkmanager.tss.bg/.venv/lib/python3.11/site-packages/netmiko/base_connection.py", line 2289, in send_config_set
    output += self.config_mode()
              ^^^^^^^^^^^^^^^^^^
  File "/var/www/networkmanager.tss.bg/.venv/lib/python3.11/site-packages/netmiko/cisco_base_connection.py", line 53, in config_mode
    return super().config_mode(
           ^^^^^^^^^^^^^^^^^^^^
  File "/var/www/networkmanager.tss.bg/.venv/lib/python3.11/site-packages/netmiko/base_connection.py", line 2124, in config_mode
    output += self.read_until_prompt(read_entire_line=True)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/var/www/networkmanager.tss.bg/.venv/lib/python3.11/site-packages/netmiko/base_connection.py", line 839, in read_until_prompt
    return self.read_until_pattern(
           ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/var/www/networkmanager.tss.bg/.venv/lib/python3.11/site-packages/netmiko/base_connection.py", line 748, in read_until_pattern
    raise ReadTimeout(msg)
netmiko.exceptions.ReadTimeout: 

Pattern not detected: '\x1b\\[KciscoS1.*' 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 test.log

ktbyers commented 1 month ago

TextFSM likely won't wors as Catalyst 1300 is not really IOS so parsing will fail.

ktbyers commented 1 month ago

This is not valid syntactically:

 net_connect.send_config_set('int gi1/0/3,no shutdown')

You need to provide a list of commands i.e.

cmds = ['int gi1/0/3', 'no shutdown']
net_connect.send_config_set(cmds)