Cisco Netmiko Scripting with Examples, networkjourney.com

Cisco Netmiko Scripting with Examples: A Comprehensive Guide

Introduction

In today’s dynamic networking landscape, automation has become a key driver of efficiency and scalability. Cisco Netmiko, a Python library, provides a powerful yet straightforward way to automate network device management. This blog post will delve into the essentials of Cisco Netmiko scripting, covering how to install it, which emulator to use, and providing 10 to 15 example scripts. We’ll also address frequently asked questions about Cisco network automation to help you get started with confidence.

What is Cisco Netmiko?

Cisco Netmiko is an open-source Python library developed by Kirk Byers, designed to simplify the automation of network devices via SSH. It extends the Paramiko library, providing a high-level interface to interact with network devices such as routers, switches, and firewalls from various vendors, including Cisco.

Installing Cisco Netmiko

Before diving into scripting, you need to install the Netmiko library. Here’s a step-by-step guide to get you started:

  1. Install Python: Ensure you have Python installed on your system. You can download it from the official Python website.
  2. Set Up a Virtual Environment (Download and Install Pycharm Community version): Create a virtual environment to manage dependencies. Download and Install Pycharm Community version
  3. Install Netmiko: Use Pycharm > Settings > Project > Python Interpreter> to install the Netmiko library.

Which Emulator to Use?

For testing and practicing Cisco Netmiko scripts, you can use network emulators or simulators. Here are a couple of popular options:

1.GNS3 (Graphical Network Simulator-3):

  • GNS3 is a robust network emulator that allows you to create complex network topologies. It supports a wide range of network devices and integrates well with real network hardware.
  • Installation Guide: GNS3 Installation

2. EVE-NG (Emulated Virtual Environment-Next Generation):

  • EVE-NG is a powerful network emulator that supports a wide range of network devices and allows for complex lab environments. It is highly regarded for its flexibility and scalability.
  • Installation Guide: EVE-NG Installation

3. PNETLab:

  • PNETLab is a comprehensive network emulator that offers a user-friendly interface and supports various network devices. It is designed to be an easy-to-use alternative to GNS3 and EVE-NG.
  • Installation Guide: PNETLab Installation

4. Cisco Modeling Labs (CML2):

  • CML2 is Cisco’s official network simulation tool, providing a realistic virtual environment for network design and testing. It supports a wide range of Cisco devices and integrates seamlessly with Cisco IOS.
  • Installation Guide: CML2 Installation

Example Cisco Netmiko Scripts

Here are some example scripts to demonstrate the capabilities of Netmiko for automating network tasks:

1. Basic Connection Script

from netmiko import ConnectHandler

cisco_device = {
    'device_type': 'cisco_ios',
    'ip': '192.168.1.1',
    'username': 'admin',
    'password': 'password',
}

net_connect = ConnectHandler(**cisco_device)
print(net_connect.find_prompt())
net_connect.disconnect()

2. Running a Single Command

from netmiko import ConnectHandler

cisco_device = {
    'device_type': 'cisco_ios',
    'ip': '192.168.1.1',
    'username': 'admin',
    'password': 'password',
}

net_connect = ConnectHandler(**cisco_device)
output = net_connect.send_command('show ip interface brief')
print(output)
net_connect.disconnect()

3. Sending Configuration Commands

from netmiko import ConnectHandler

cisco_device = {
    'device_type': 'cisco_ios',
    'ip': '192.168.1.1',
    'username': 'admin',
    'password': 'password',
}

net_connect = ConnectHandler(**cisco_device)
config_commands = [
    'interface GigabitEthernet0/1',
    'description Connected to LAN',
    'ip address 192.168.2.1 255.255.255.0',
    'no shutdown',
]
output = net_connect.send_config_set(config_commands)
print(output)
net_connect.disconnect()

4. Running Multiple Commands Sequentially

from netmiko import ConnectHandler

cisco_device = {
    'device_type': 'cisco_ios',
    'ip': '192.168.1.1',
    'username': 'admin',
    'password': 'password',
}

net_connect = ConnectHandler(**cisco_device)
commands = ['show version', 'show ip route', 'show interfaces']
for command in commands:
    output = net_connect.send_command(command)
    print(output)
net_connect.disconnect()

5. Backup Device Configuration

from netmiko import ConnectHandler

cisco_device = {
    'device_type': 'cisco_ios',
    'ip': '192.168.1.1',
    'username': 'admin',
    'password': 'password',
}

net_connect = ConnectHandler(**cisco_device)
output = net_connect.send_command('show running-config')
with open('backup_config.txt', 'w') as file:
    file.write(output)
net_connect.disconnect()

6. VLAN Configuration

from netmiko import ConnectHandler

cisco_device = {
    'device_type': 'cisco_ios',
    'ip': '192.168.1.1',
    'username': 'admin',
    'password': 'password',
}

net_connect = ConnectHandler(**cisco_device)
config_commands = [
    'vlan 10',
    'name Marketing',
    'exit',
    'vlan 20',
    'name Sales',
    'exit',
]
output = net_connect.send_config_set(config_commands)
print(output)
net_connect.disconnect()

7. Looping Through Multiple Devices

from netmiko import ConnectHandler

devices = [
    {
        'device_type': 'cisco_ios',
        'ip': '192.168.1.1',
        'username': 'admin',
        'password': 'password',
    },
    {
        'device_type': 'cisco_ios',
        'ip': '192.168.1.2',
        'username': 'admin',
        'password': 'password',
    },
]

for device in devices:
    net_connect = ConnectHandler(**device)
    output = net_connect.send_command('show ip interface brief')
    print(f"Device {device['ip']}:\n{output}")
    net_connect.disconnect()

8. Interface Status Check

from netmiko import ConnectHandler

cisco_device = {
    'device_type': 'cisco_ios',
    'ip': '192.168.1.1',
    'username': 'admin',
    'password': 'password',
}

net_connect = ConnectHandler(**cisco_device)
output = net_connect.send_command('show interfaces status')
print(output)
net_connect.disconnect()

9. Applying ACLs

from netmiko import ConnectHandler

cisco_device = {
    'device_type': 'cisco_ios',
    'ip': '192.168.1.1',
    'username': 'admin',
    'password': 'password',
}

net_connect = ConnectHandler(**cisco_device)
config_commands = [
    'access-list 100 permit ip any any',
    'access-list 101 deny ip 192.168.1.0 0.0.0.255 any',
]
output = net_connect.send_config_set(config_commands)
print(output)
net_connect.disconnect()

10. BGP Configuration

from netmiko import ConnectHandler

cisco_device = {
    'device_type': 'cisco_ios',
    'ip': '192.168.1.1',
    'username': 'admin',
    'password': 'password',
}

net_connect = ConnectHandler(**cisco_device)
config_commands = [
    'router bgp 65000',
    'neighbor 192.168.2.1 remote-as 65001',
    'network 192.168.1.0 mask 255.255.255.0',
]
output = net_connect.send_config_set(config_commands)
print(output)
net_connect.disconnect()

11. Checking Device Uptime

from netmiko import ConnectHandler

cisco_device = {
    'device_type': 'cisco_ios',
    'ip': '192.168.1.1',
    'username': 'admin',
    'password': 'password',
}

net_connect = ConnectHandler(**cisco_device)
output = net_connect.send_command('show version | include uptime')
print(output)
net_connect.disconnect()

12. Loopback Interface Configuration

from netmiko import ConnectHandler

cisco_device = {
    'device_type': 'cisco_ios',
    'ip': '192.168.1.1',
    'username': 'admin',
    'password': 'password',
}

net_connect = ConnectHandler(**cisco_device)
config_commands = [
    'interface Loopback0',
    'ip address 10.0.0.1 255.255.255.255',
    'no shutdown',
]
output = net_connect.send_config_set(config_commands)
print(output)
net_connect.disconnect()

13. Monitoring CPU Usage

from netmiko import ConnectHandler

cisco_device = {
    'device_type': 'cisco_ios',
    'ip': '192.168.1.1',
    'username': 'admin',
    'password': 'password',
}

net_connect = ConnectHandler(**cisco_device)
output = net_connect.send_command('show processes cpu')
print(output)
net_connect.disconnect

()

14. NTP Server Configuration

from netmiko import ConnectHandler

cisco_device = {
    'device_type': 'cisco_ios',
    'ip': '192.168.1.1',
    'username': 'admin',
    'password': 'password',
}

net_connect = ConnectHandler(**cisco_device)
config_commands = [
    'ntp server 192.168.2.1',
]
output = net_connect.send_config_set(config_commands)
print(output)
net_connect.disconnect()

15. DHCP Pool Configuration

from netmiko import ConnectHandler

cisco_device = {
    'device_type': 'cisco_ios',
    'ip': '192.168.1.1',
    'username': 'admin',
    'password': 'password',
}

net_connect = ConnectHandler(**cisco_device)
config_commands = [
    'ip dhcp pool MY_POOL',
    'network 192.168.1.0 255.255.255.0',
    'default-router 192.168.1.1',
    'dns-server 8.8.8.8 8.8.4.4',
]
output = net_connect.send_config_set(config_commands)
print(output)
net_connect.disconnect()

Frequently Asked Questions (FAQs) about Cisco Network Automation

1. What are the prerequisites for learning Cisco Netmiko?

Answer:

  • Basic understanding of networking concepts.
  • Familiarity with Cisco devices and CLI commands.
  • Basic knowledge of Python programming.

2. Can Netmiko be used with non-Cisco devices?

Answer:

  • Yes, Netmiko supports various vendors including Juniper, Arista, HP, and more.
  • You can specify the device_type parameter for different vendors.

3. Is Netmiko suitable for production environments?

Answer:

  • Netmiko is widely used in production environments for automating routine tasks.
  • However, always test scripts in a lab environment before deploying them in production.

4. How does Netmiko handle SSH connections?

Answer:

  • Netmiko extends Paramiko, providing a high-level interface for SSH connections.
  • It handles the complexities of SSH, making it easier to send commands and receive outputs.

5. What are the alternatives to Netmiko?

Answer:

  • NAPALM (Network Automation and Programmability Abstraction Layer with Multivendor support): A library that supports multiple vendors and provides a common API.
  • Ansible: An IT automation tool that can be used for network automation.
  • PyATS (Python Automation Test System): A Python-based test framework developed by Cisco.

Conclusion

Cisco Netmiko scripting is a powerful tool for automating network device management, making it easier to execute routine tasks, ensure configuration consistency, and improve overall efficiency. By following the steps outlined in this blog post, you can get started with Netmiko, utilize emulators for testing, and implement a variety of useful scripts. Armed with this knowledge, you’re well on your way to becoming proficient in network automation, driving both personal and organizational growth.