Automating Cisco Router Configuration with Python and Paramiko

Automating Cisco Router Configuration with Python and Paramiko


In the dynamic landscape of network management, automating routine tasks can significantly enhance efficiency and reduce the likelihood of errors. This post introduces a Python script powered by the Paramiko library, designed to automate the login and configuration process for Cisco routers. We’ll explore how this script establishes an SSH connection, logs in, enters privileged mode, and configures a loopback interface—all with just a few lines of code.

Script Overview:

# Import the Paramiko library for SSH communication
import paramiko
# Import the time module for sleep functionality
import time

# Define a function for Cisco login
def cisco_login(hostname, username, password, enable_password):
    # Create an SSH client
    ssh = paramiko.SSHClient()

    # Automatically add the server's host key (this is insecure, see comments below)

        # Connect to the router using provided credentials
        ssh.connect(hostname, username=username, password=password, allow_agent=False, look_for_keys=False)

        # Create a shell channel for interaction
        channel = ssh.invoke_shell()

        # Wait for the prompt after connecting
        # Receive and decode the initial output
        output = channel.recv(65535).decode('utf-8')

        # Check if login was successful by looking for '#' or '>'
        if '#' in output or '>' in output:
            # Send the 'enable' command to enter privileged mode

            # Send the enable password
            channel.send(enable_password + "\n")

            # Check if the prompt indicates successful enable mode
            enable_output = channel.recv(65535).decode('utf-8')

            # Check if '#' is present in enable mode prompt
            if '#' in enable_output:
                print("Login successful! Entered privilege mode.")

                # Show initial IP interface brief
                channel.send("show ip interface brief\n")
                ip_interface_brief_output = channel.recv(65535).decode('utf-8')

                # Configure loopback interface
                channel.send("conf t\n")
                channel.send("int loopback 0\n")
                channel.send("ip address\n")
                # Show IP interface brief after configuration
                channel.send("show ip interface brief\n")
                ip_interface_brief_output = channel.recv(65535).decode('utf-8')

                print("Enable mode failed!")
                print("Login failed!")

            print("Login failed!")

        # Close the SSH connection

    except Exception as e:
        # Handle exceptions and print an error message
        print(f"Error: {e}")
        print("Login failed!")

# Entry point of the script
if __name__ == "__main__":
    # Replace these values with your router's information
    router_hostname = ""
    router_username = "cisco"
    router_password = "cisco@123"
    router_enable_password = "cisco@123"

    # Call the function to attempt login
    cisco_login(router_hostname, router_username, router_password, router_enable_password)


  1. SSH Connection and Login: The script establishes an SSH connection to the Cisco router, logs in, and enters privileged mode.
  2. Show Initial IP Interface Brief: It shows the initial ‘show ip interface brief’ output to provide a snapshot of the router’s interfaces.
  3. Configure Loopback Interface: The script enters configuration mode, creates a loopback interface, assigns an IP address, and ends the configuration.
  4. Show IP Interface Brief After Configuration: Finally, it displays the ‘show ip interface brief’ output again, showcasing the changes made to the router’s interfaces.

Conclusion: Automating Cisco router configurations with Python and Paramiko simplifies network management tasks. This script serves as a foundation, demonstrating how automation can streamline the login and configuration process, saving time and reducing the risk of errors in a network environment.


Here is the YouTube video:


You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *