Python Automation - Script 1

"Let us configure a Python script that will connect to a Cisco router and execute the show running-config command to display the current running confi

This instruction clearly outlines the objective of creating a script to communicate with a Cisco router and retrieve the configuration details using the specified command.

First, ensure that you've installed the Netmiko library. You can do this by running:

pip install netmiko

Next, you can use the following script as a starting point. Make sure to modify the connection parameters according to your setup:

from netmiko import ConnectHandler

# Define the device parameters
cisco_router = {
    'device_type': 'cisco_ios', # If you are using a different Cisco OS, change this accordingly
    'host': '192.168.1.1',       # The IP address of your router
    'username': 'username',
    'password': 'password',
}

# Connect to the router
connection = ConnectHandler(**cisco_router)

# Send the command 'show running-config'
output = connection.send_command('show running-config')

# Print the output
print(output)

# Disconnect from the router
connection.disconnect()

Remember to replace '192.168.1.1', 'username', and 'password' with your router's actual IP address, username, and password.

Note: Running the show running-config command might require privileged access, depending on the router's configuration. You may need to modify the script to include logic that enables privileged mode if it's necessary for your particular setup.

let's break down the code snippet above to understand what's happening at each step:

  1. Importing the Netmiko Library:

     from netmiko import ConnectHandler
    

    This imports the ConnectHandler class from the Netmiko library, which allows us to create a connection to the Cisco device.

  2. Defining the Device Parameters:

     cisco_router = {
         'device_type': 'cisco_ios', # The device type you're connecting to
         'host': '192.168.1.1',       # The IP address of your router
         'username': 'username',
         'password': 'password',
     }
    

    Here, you create a dictionary containing the connection parameters for your Cisco router. This includes the device type, IP address, username, and password.

  3. Connecting to the Router:

     connection = ConnectHandler(**cisco_router)
    

    Using the ConnectHandler function and the parameters you defined earlier, this line creates a connection to the router. The **cisco_router syntax is unpacking the dictionary into keyword arguments for the function.

  4. Sending the Command 'show running-config':

     output = connection.send_command('show running-config')
    

    This sends the show running-config command to the router and saves the output to the variable output.

  5. Printing the Output:

     print(output)
    

    This prints the output of the command to the terminal, allowing you to see the running configuration of the router.

  6. Disconnecting from the Router:

     connection.disconnect()
    

    This line closes the SSH connection to the router, which is a good practice once you are done with your operations.

Overall, this script provides a simple and effective way to retrieve the running configuration from a Cisco router. Make sure to replace the IP address, username, and password with your device's actual credentials, and always be cautious when running scripts like this on production devices.

Check my previous blogs to see how to install Python on windows and Linux. Also another blog which explains Netmiko and Parmiko in detail.

Happy Learning