In Part 1 of this series, we saw how to establish a connection with network devices using python netmiko and fetch command outputs. In this post, we will see how to enter into enable mode and also configure cisco devices using python netmiko programmatically.
If you are new to netmiko, please read Part1 here
all_devices = [csr1000v1]
for device in all_devices:
net_connect = ConnectHandler(**device)
print(dir(net_connect))
## There are tons of methods that you can call on net_connect connection object that we just created.
─ python3 script4.py ─╯
['RESPONSE_RETURN',
'RETURN',
'TELNET_RETURN',
'__class__',
'__delattr__',
'__dict__',
'__dir__',
'__doc__',
'__enter__',
'__eq__',
'__exit__',
'__format__',
'__ge__',
'__getattribute__',
'__gt__',
'__hash__',
'__init__',
'__init_subclass__',
'__le__',
'__lt__',
'__module__',
'__ne__',
'__new__',
'__reduce__',
'__reduce_ex__',
'__repr__',
'__setattr__',
'__sizeof__',
'__str__',
'__subclasshook__',
'__weakref__',
'_autodetect_fs',
'_build_ssh_client',
'_config_mode',
'_connect_params_dict',
'_first_line_handler',
'_legacy_mode',
'_lock_netmiko_session',
'_modify_connection_params',
'_open',
'_read_channel',
'_read_channel_expect',
'_read_channel_timing',
'_sanitize_output',
'_session_locker',
'_session_log_close',
'_session_log_fin',
'_test_channel_read',
'_timeout_exceeded',
'_try_session_preparation',
'_unlock_netmiko_session',
'_use_ssh_config',
'_write_channel',
'_write_session_log',
'allow_agent',
'allow_auto_change',
'alt_host_keys',
'alt_key_file',
'ansi_escape_codes',
'auth_timeout',
'banner_timeout',
'base_prompt',
'blocking_timeout',
'check_config_mode',
'check_enable_mode',
'cleanup',
'clear_buffer',
'close_session_log',
'commit',
'config_mode',
'conn_timeout',
'device_type',
'disable_paging',
'disconnect',
'enable',
'encoding',
'establish_connection',
'exit_config_mode',
'exit_enable_mode',
'fast_cli',
'find_prompt',
'global_cmd_verify',
'global_delay_factor',
'host',
'is_alive',
'keepalive',
'key_file',
'key_policy',
'normalize_cmd',
'normalize_linefeeds',
'open_session_log',
'paramiko_cleanup',
'passphrase',
'password',
'pkey',
'port',
'protocol',
'read_channel',
'read_until_pattern',
'read_until_prompt',
'read_until_prompt_or_pattern',
'remote_conn',
'remote_conn_pre',
'run_ttp',
'save_config',
'secret',
'select_delay_factor',
'send_command',
'send_command_expect',
'send_command_timing',
'send_config_from_file',
'send_config_set',
'serial_login',
'serial_settings',
'session_log',
'session_log_record_writes',
'session_preparation',
'session_timeout',
'set_base_prompt',
'set_terminal_width',
'sock',
'special_login_handler',
'ssh_config_file',
'strip_ansi_escape_codes',
'strip_backspaces',
'strip_command',
'strip_prompt',
'system_host_keys',
'telnet_login',
'timeout',
'use_keys',
'username',
'verbose',
'write_channel']
For this example, we need to focus on find_prompt() and enable()
- find_prompt( ) will allow you to print the current prompt.
- enable( ) will allow you to enter the enable prompt.
- config( ) will allow you to enter the config prompt and execute config commands.
net_connect = ConnectHandler(**device)
print(net_connect.find_prompt())
net_connect.enable()
print(net_connect.find_prompt())
net_connect.config_mode()
print(net_connect.find_prompt())
╰─ python3 script4.py ─╯
csr1000v-1#
csr1000v-1#
csr1000v-1(config)#
Since it’s a cisco dev box, you don’t see the ‘>’ prompt most likely due to priv lvl 15 account. But if you do this on your lab device, you can see the ‘>’ prompt before you enter the enable( ) command.
Once you are inside the config mode( ), we can push the configuration changes to command to devices. Since this is a lab box and we are responsible, we will only make non-intrusive configuration changes to demonstrate this example. Let’s see how to configure cisco devices using python netmiko
net_connect = ConnectHandler(**device)
print(net_connect.find_prompt())
net_connect.enable()
print(net_connect.find_prompt())
net_connect.config_mode()
print(net_connect.find_prompt())
print(net_connect.send_command('do show int description'))
print("======================================")
print(net_connect.send_config_set(['interface Gi2', 'description networkautomationlane.in']))
print("======================================")
print(net_connect.send_command('show int description'))
print("======================================")
╰─ python3 script4.py ─╯
csr1000v-1#
csr1000v-1#
csr1000v-1(config)#
Interface Status Protocol Description
Gi1 up up MANAGEMENT INTERFACE - DON'T TOUCH ME
Gi2 admin down down Network Interface
Gi3 admin down down Network Interface
======================================
interface Gi2
csr1000v-1(config-if)#description networkautomationlane.in
csr1000v-1(config-if)#end
csr1000v-1#
======================================
Interface Status Protocol Description
Gi1 up up MANAGEMENT INTERFACE - DON'T TOUCH ME
Gi2 admin down down networkautomationlane.in
Gi3 admin down down Network Interface
======================================
Explanation:-
- Enter enable mode
- Enter config mode
- execute “do show int description” because we are inside config mode.
- Push config commands to change the description of Gi2 to networkautomationlane.in
- Once the push of config commands is complete, netmiko will automatically exit the config mode.
- Execute ‘show int description’ because we are in enable mode to see the configuration change being applied.
Since we are responsible, we will revert it back to the state it was prior to making any config change.
net_connect = ConnectHandler(**device)
print(net_connect.find_prompt())
net_connect.enable()
print(net_connect.find_prompt())
net_connect.config_mode()
print(net_connect.find_prompt())
print(net_connect.send_command('do show int description'))
print("======================================")
print(net_connect.send_config_set(['interface Gi2', 'description Network Interface']))
print("======================================")
print(net_connect.send_command('show int description'))
print("======================================")
╰─ python3 script4.py ─╯
csr1000v-1#
csr1000v-1#
csr1000v-1(config)#
Interface Status Protocol Description
Gi1 up up MANAGEMENT INTERFACE - DON'T TOUCH ME
Gi2 admin down down networkautomationlane.in
Gi3 admin down down Network Interface
======================================
interface Gi2
csr1000v-1(config-if)#description Network Interface
csr1000v-1(config-if)#end
csr1000v-1#
======================================
Interface Status Protocol Description
Gi1 up up MANAGEMENT INTERFACE - DON'T TOUCH ME
Gi2 admin down down Network Interface
Gi3 admin down down Network Interface
======================================