chun xu
chun xu

Reputation: 433

netmiko connect via remote console port

We are using opengear to connect Cisco devices via the console port remotely, I need to put some configuration through the console port. I refer to this link Netmiko - Enter configuration terminal mode and wrote below codes. When I run the code, it throw the error, but if I debug the code using pycharm, it works. From the device console, I can see if I run the code under debug mode, the device is activated and get into enable mode, but if I run the code directly, there is no response from the device.

Is there anything that I have missed?

from netmiko import ConnectHandler, redispatch
device = {
    "device_type": "terminal_server",
    "ip": "****",
    "username": "****",
    "password": "****",
    "session_log": 'netmiko_session.log',
    "port": '22'
}

net_connect = ConnectHandler(**device)
net_connect.write_channel("\r")

net_connect.enable()
net_connect.send_command_timing("enable")
redispatch(net_connect, device_type="cisco_xe")
output = net_connect.send_command("show version")
print(output)

net_connect.disconnect()

Here is error

Traceback (most recent call last):
  File "C:\Users\user\venv\console_conn.py", line 24, in <module>
    redispatch(net_connect, device_type="cisco_xe")
  File "C:\Users\user\venv\lib\site-packages\netmiko\ssh_dispatcher.py", line 487, in redispatch
    obj._try_session_preparation()
  File "C:\Users\user\venv\lib\site-packages\netmiko\base_connection.py", line 963, in _try_session_preparation
    self.session_preparation()
  File "C:\Users\user\venv\lib\site-packages\netmiko\cisco\cisco_ios.py", line 19, in session_preparation
    self.set_terminal_width(command=cmd, pattern=cmd)
  File "C:\Users\user\lib\site-packages\netmiko\base_connection.py", line 1322, in set_terminal_width
    output = self.read_until_pattern(pattern=pattern)
  File "C:\Users\user\venv\lib\site-packages\netmiko\base_connection.py", line 721, in read_until_pattern
    raise ReadTimeout(msg)
netmiko.exceptions.ReadTimeout: 

Pattern not detected: 'terminal width 511' 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.

Upvotes: 0

Views: 451

Answers (1)

Aero3511
Aero3511

Reputation: 1

Had simmilar issue with Arista EOS using netmiko.

In log found

$cat log.txt 

Last login: Thu Mar 14 09:16:27 2024 from 10.0.1.251

waiting for mounts to complete ...ok
hostname>
hostname>terminal width 511
% Invalid input
hostname>
hostname>exit

After this set fast_cli and auto_connect to False and addedestablish_connection method to my ssh connection.

from netmiko import ConnectHandler

params = {
    'host': 'hostname',
    'username': 'user',
    'password': 'pass',
    'device_type': 'arista_eos',
    'fast_cli': False,
    'auto_connect': False,
}

ssh = ConnectHandler(**params, session_log='log.txt')
ssh.establish_connection()
ssh.enable()
command = ssh.send_command('show version')

if ssh.is_alive():
    print(command)

Hope, this will help if it`s still actual

Upvotes: 0

Related Questions