Skip to content

Network Automation

My journey with Network & Cloud Automation

Menu
  • Beginner
  • DevOps-NetDevOps
  • Network Automation
    • Docker
    • Python Libraries
      • NAPALM
      • Netmiko
      • Jinja2
      • Scrapli
      • Yang
  • Cloud Automation
    • Terraform
  • Python 🐍 Tips and Tricks
Menu
Netmiko TEXTFSM

How to Automate Network Devices using Python Netmiko Part 2

Posted on August 11, 2021January 30, 2022 by Gurpreet Kochar

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

How to Automate Network Devices using Python Netmiko Part I
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()

  1. find_prompt( ) will allow you to print the current prompt.
  2. enable( ) will allow you to enter the enable prompt.
  3. 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
======================================

Know someone who may benefit? Share this:

  • Tweet
  • Click to share on Telegram (Opens in new window) Telegram
  • Click to share on WhatsApp (Opens in new window) WhatsApp
  • Click to email a link to a friend (Opens in new window) Email
  • More
  • Click to print (Opens in new window) Print
  • Click to share on Reddit (Opens in new window) Reddit
  • Share on Tumblr
  • Pocket

Like this:

Like Loading...

Related

Leave a ReplyCancel reply

All Blog Posts
My Resume

Upcoming Posts

Sorry - nothing planned yet!

Recent Posts

  • How to backup configuration to TFTP Server using Ansible – Part II
  • How to backup network devices using Ansible – Part I
  • Netmiko SSH Proxy/JumpServer
  • A short note on SASE
  • Understanding Ansible

Recent Comments

  1. Jack on Multithreading with Python for Network Engineers
  2. LifeCanvas on [Theory] Multithreading vs Multiprocessing vs AsyncIO
  3. Jasper Horng on Netmiko SSH Proxy/JumpServer
  4. asdfasdf on Python API Using FASTAPI – UPDATE – PUT – PATCH – Part V
  5. Gurpreet Kochar on Python Scrapli AsyncIO Usage

Archives

  • September 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
Topic Request / Suggestion
Loading
© 2025 Network Automation | Powered by Minimalist Blog WordPress Theme
%d