Python for Network Automation - Part 4 (Netmiko)

Introduction

Welcome to Part 4 of our "Python for Network Automation" series. As we continue our journey, we turn our focus to Netmiko*, a powerful tool that has transformed the way network engineers interact with devices.

This Blog will provide a comprehensive look into Netmiko, making it easier for both newcomers and experienced professionals to harness its capabilities.

Let's dive in and explore the world of Netmiko in network automation!

Understanding the Screen-Scraping Dilemma

Before we delve into the advanced features of Netmiko, it's crucial to grasp the challenges it addresses. Screen-scraping, a method of extracting data directly from device output screens, is a common technique in network automation. However, it's not without its pitfalls:

1. Unstructured Data: Traditional screen-scraping returns a monolithic block of unstructured text. This makes extracting specific information a tedious task, leading to inefficient and error-prone scripts.

2. Ambiguous Command Completion: When a command is dispatched to a device, there's no clear indicator that the command has finished executing. Many automation scripts use the device's prompt as a sign, but this isn't always reliable.
 

Enter Netmiko

Netmiko, an extension of the Paramiko library, is meticulously crafted for network devices. It offers a structured way to interact with devices over SSH, ensuring clarity in the returned data and reliability in command execution.

Consider this basic Netmiko script:

from netmiko import ConnectHandler
from getpass import getpass
host = input("Enter your hostname: ")
device = { 
    'device_type': 'cisco_ios',
    'host': host,
    'username': 'pyclass',
    'password': getpass(),

net_connect = ConnectHandler(**device)
output = net_connect.send_command("show ip int brief")
print(output)

Netmiko's Advanced Features

Netmiko isn't just about sending commands and receiving outputs. It's a versatile tool equipped with features to handle a wide range of scenarios:
 

1. Handling Prompting

Scenario: You're automating the backup of configurations across numerous devices. Some devices, due to security protocols, prompt for confirmation before granting access to configuration files.

Netmiko Solution: Use the `expect_string` parameter to anticipate these prompts. By specifying the expected string, Netmiko waits for it and can then send the necessary response.

cmd = 'copy running-config startup-config'
output = net_connect.send_command(cmd, expect_string=r'Save?')
output += net_connect.send_command('yes')

2. Delay Factor

Scenario: Monitoring a global network means dealing with devices that, due to varying network conditions, might respond with delays, causing premature script timeouts.

Netmiko Solution: The `delay_factor` allows adjustment of the delay for specific commands, ensuring the script waits adequately

output = net_connect.send_command('show ip bgp summary', delay_factor=4)

3. Integration with TextFSM

Scenario: Extracting specific data from `show` command outputs, like interface statuses or BGP neighbor details, and presenting them in a structured format.

Netmiko Solution: Integration with TextFSM transforms unstructured command outputs into structured Python data structures.

output = net_connect.send_command('show interfaces', use_textfsm=True)

4. Config Operations

Scenario: A new security policy requires disabling unused ports across all switches.

Netmiko Solution: The `send_config_set` method allows sending a list of configuration commands.

config_commands = ['interface range Gi0/1-24', 'shutdown']

output = net_connect.send_config_set(config_commands)

5. Secure Copy

Scenario: Updating firmware on all routers.

Netmiko Solution: Using the `FileTransfer` class, automate the firmware transfer.


from netmiko import FileTransfer

with FileTransfer(net_connect, source_file='new_firmware.bin', dest_file='flash:/new_firmware.bin') as scp_transfer:
    if not scp_transfer.check_file_exists():
        scp_transfer.transfer_file()

6. Other Advanced Features

- Handling Multiple Prompts: Chain multiple `expect_string` parameters for sequential prompts.

- Dynamic Delay Factor: Profile device response times and adjust the `delay_factor` dynamically.

- Integration with Other Tools: Combine Netmiko's structured outputs with monitoring tool APIs or database connectors.
 

7. Netmiko program: Incorporating the features discussed above in the blog:

from netmiko import ConnectHandler, FileTransfer
from getpass import getpass

def main():
    # User input
    host = input("Enter your hostname: ")
    username = input("Enter your username: ")
    password = getpass("Enter your password: ")

    # Device dictionary
    device = {
        'device_type': 'cisco_ios',
        'host': host,
        'username': username,
        'password': password,
    }

    # Establishing connection
    net_connect = ConnectHandler(**device)

    # 1. Handling Prompting
    cmd = 'copy running-config startup-config'
    output = net_connect.send_command(cmd, expect_string=r'Save?')
    output += net_connect.send_command('yes')
    print(output)

    # 2. Delay Factor
    output = net_connect.send_command('show ip bgp summary', delay_factor=4)
    print(output)

    # 3. Integration with TextFSM
    output = net_connect.send_command('show interfaces', use_textfsm=True)
    print(output)

    # 4. Config Operations
    config_commands = ['interface range Gi0/1-24', 'shutdown']
    output = net_connect.send_config_set(config_commands)
    print(output)

    # 5. Secure Copy
    source_file = 'new_firmware.bin'
    dest_file = 'flash:/new_firmware.bin'
    with FileTransfer(net_connect, source_file=source_file, dest_file=dest_file) as scp_transfer:
        if not scp_transfer.check_file_exists():
            scp_transfer.transfer_file()
            print(f"Transferred {source_file} to {dest_file}")

    # 6. Other Advanced Features
    # Handling Multiple Prompts
    cmd = 'reset command example'  # Replace with a command that prompts multiple times
    output = net_connect.send_command(cmd, expect_string=r'First Prompt?')
    output += net_connect.send_command('yes', expect_string=r'Second Prompt?')
    output += net_connect.send_command('yes')
    print(output)

    # Dynamic Delay Factor (Example: Adjusting delay based on device type or location)
    if 'specific_device_criteria' in host:  # Replace with actual criteria
        output = net_connect.send_command('show version', delay_factor=5)
    else:
        output = net_connect.send_command('show version')
    print(output)

    # Integration with Other Tools
    # This is a placeholder. You'd typically integrate the structured data with other systems or databases here.

    # Closing the connection
    net_connect.disconnect()

if __name__ == "__main__":
    main()

Let's break down the provided Python program and explain its significance for a network engineer.

Program Overview:

This Python program uses the `Netmiko` library to automate various tasks on network devices. The tasks include saving configurations, fetching BGP summaries, shutting down interfaces, transferring files, and more.

Detailed Explanation:

1. User Input:

The program starts by asking the user for the hostname, username, and password of the device they wish to connect to. This information is essential for establishing an SSH connection to the device.

2. Establishing Connection:

Using the `ConnectHandler` function from `Netmiko`, the program establishes an SSH connection to the device using the provided credentials.

3. Handling Prompting:

Some commands, when executed on network devices, prompt the user for additional input. For instance, when saving a running configuration to startup, the device might ask for confirmation. The program demonstrates how to handle such prompts using Netmiko's `expect_string` parameter.

4. Delay Factor:

In networks with devices located globally or under varying conditions, responses can be delayed. The program showcases how to use the `delay_factor` to ensure the script waits long enough for a response, preventing premature timeouts.

5. Integration with TextFSM:

Network engineers often need to parse the output of `show` commands to extract specific information. The program demonstrates how to transform unstructured command outputs into structured Python data structures using TextFSM integration.

6. Config Operations:

Automating configuration tasks is a common requirement for network engineers. The program illustrates how to send a set of configuration commands to a device, like shutting down a range of interfaces.

7. Secure Copy:

Transferring files, like firmware or configuration backups, is another routine task. The program uses Netmiko's SCP capabilities to automate the firmware transfer process.

8. Advanced Features:

Handling Multiple Prompts: Some commands might lead to multiple prompts. The program provides a placeholder example of how to handle such scenarios.

Dynamic Delay Factor: Depending on the device or its location, different delay factors might be needed. The program gives an example of adjusting the delay dynamically.

Integration with Other Tools: This is a placeholder in the program. In real-world scenarios, network engineers might integrate the fetched data with monitoring tools, databases, or other systems.

9. Closing the Connection:

After executing all tasks, the program disconnects from the device.

Significance for Network Engineers:

-Efficiency: Automating repetitive tasks saves time and ensures consistency across devices.

- Reliability: By scripting tasks, human errors can be minimized. For instance, shutting down the wrong interface or missing a step in a manual process.

- Scalability: A script like this can be expanded to handle multiple devices, making it easier to manage large networks.

- Data Parsing: With TextFSM integration, engineers can easily extract, analyze, and report specific pieces of information from command outputs.

- Flexibility: The program can be adapted to various scenarios, from configuration backups to firmware updates, making it a versatile tool in a network engineer's toolkit.

In summary, such a program empowers network engineers to manage and maintain their networks more effectively, reducing manual effort and the potential for errors.

Conclusion:

In this comprehensive blog on Netmiko, we've journeyed through the nuances of network automation, shedding light on the challenges faced by network engineers and the solutions that Netmiko brings to the table. From handling the intricacies of screen-scraping to managing command prompts and delays, Netmiko proves to be an indispensable tool for modern network engineers. Its seamless integration with TextFSM and its robust set of features, such as secure file transfer and configuration operations, further solidify its position as a must-have in the network automation toolkit.

But the world of network automation is vast and ever-evolving. As we continue to explore this domain, more advanced topics, tools, and techniques await. So, to all our readers, whether you're a seasoned network engineer or a budding enthusiast, stay tuned for more deep dives into the realm of network automation. Until then, happy reading and happy coding!