I am trying to use Netmiko and work in 'configuration terminal' mode. Why there only 'enable' mode function and not 'configuration terminal' mode.? (i.e : net_connect.enable())
My program should do these steps:
Editing I tried the following command to enter config_mode and got 'ValueError'.
net_connect.find_prompt()
net_connect.enable()
net_connect.find_prompt()
net_connect.config_mode()
net_connect.find_prompt()
Logs:
>>> from netmiko import ConnectHandler
>>> net_connect = ConnectHandler(device_type='cisco_ios', host='r-hpc-sw19', username='admin', password='admin')
>>> net_connect.find_prompt()
'r-hpc-sw19 [standalone: master] >'
>>> net_connect.enable()
'enable\r\r\n\rr-hpc-sw19 [standalone: master] # '
>>> net_connect.find_prompt()
'r-hpc-sw19 [standalone: master] #'
>>> net_connect.config_mode()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/labhome/arielwe/.local/lib/python3.9/site-packages/netmiko/cisco_base_connection.py", line 48, in config_mode
return super().config_mode(
File "/labhome/arielwe/.local/lib/python3.9/site-packages/netmiko/base_connection.py", line 1766, in config_mode
raise ValueError("Failed to enter configuration mode.")
ValueError: Failed to enter configuration mode.
Second Try:
from netmiko import ConnectHandler
net_connect = ConnectHandler(device_type='cisco_ios', host='r-hpc-sw19', username='admin', password='admin')
net_connect.find_prompt()
net_connect.enable()
net_connect.find_prompt()
cfg = net_connect.send_config_set(["show version | json-print"])
net_connect.find_prompt()
>>> from netmiko import ConnectHandler
>>> net_connect = ConnectHandler(device_type='cisco_ios', host='r-hpc-sw19', username='admin', password='admin')
>>> net_connect.find_prompt()
'r-hpc-sw19 [standalone: master] >'
>>> net_connect.enable()
'enable\r\r\n\rr-hpc-sw19 [standalone: master] # '
>>> net_connect.find_prompt()
'r-hpc-sw19 [standalone: master] #'
>>> cfg = net_connect.send_config_set(["show version | json-print"])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/labhome/arielwe/.local/lib/python3.9/site-packages/netmiko/base_connection.py", line 1876, in send_config_set
output += self.config_mode(*cfg_mode_args)
File "/labhome/arielwe/.local/lib/python3.9/site-packages/netmiko/cisco_base_connection.py", line 48, in config_mode
return super().config_mode(
File "/labhome/arielwe/.local/lib/python3.9/site-packages/netmiko/base_connection.py", line 1766, in config_mode
raise ValueError("Failed to enter configuration mode.")
ValueError: Failed to enter configuration mode.
>>> net_connect.find_prompt()
Thanks, Ariel.
Why there only 'enable' mode function and not 'configuration terminal' mode.?
There is a configure terminal
mode in netmiko
, using conn.config_mode()
sends configure terminal
to the remote device.
However, you can use send_config_set()
which enters configuration mode by default and exits it after the last command was sent. An example of using send_config_set()
from netmiko import ConnectHandler
device = {
"device_type": "cisco_ios",
"ip": "192.168.1.1",
"username": "cisco",
"password": "cisco",
"secret": "cisco",
"fast_cli": False,
}
with ConnectHandler(**device) as conn:
if not conn.check_enable_mode():
conn.enable()
cfg = conn.send_config_set(["interface Loopback 212", "shutdown"])
print(cfg)
RTR1(config)#configure terminal
Enter configuration commands, one per line. End with CNTL/Z.
RTR1(config)#interface Loopback 212
RTR1(config-if)#shutdown
RTR1(config-if)#end
RTR1#
EDIT
Since neither send_config_set()
nor config_mode()
work, you can use send_command_timing()
and write the command yourself instead of using the methods in netmiko.
send_command_timing()
is delay-based and not pattern-based like send_command()
. the timing method waits for sometime and sends the command after, it searches for no patterns. You can also read the first part of this documentation link to know more about send_command_timing()
net_conn.send_command_timing("enable")
net_conn.send_command_timing("configure terminal")