Home  >  Article  >  Backend Development  >  Cisco Automation with Python

Cisco Automation with Python

WBOY
WBOYOriginal
2024-09-07 14:32:17856browse

Automatización de Cisco con Python

Automation of network devices, such as Cisco routers and switches, can be easily achieved using Python with the Netmiko library, designed to easily handle SSH connections. Next, I show you two basic scripts to view the configuration of a Cisco device and to automate the creation and configuration of VLANs.

1. Script to view the configuration of a Cisco device (router):

from netmiko import ConnectHandler
ssh=ConnectHandler(
    device_type="cisco_ios",
    host="192.168.0.10",
    port=22,
    username="cisco",
    password="cisco"
)
out=ssh.send_command("show run")
print(ssh.find_prompt())
print("show run:\n"+out)

Operation:

  • Netmiko Import: The ConnectHandler class is imported to handle the SSH connection.
  • SSH Connection: Using the IP, port and credentials of the Cisco device, a connection is created.
  • Command execution: The show run command is sent to obtain the current configuration of the device.
  • Prompt printing: The device prompt is printed, indicating that the connection is still active.
  • Configuration display: The command output is printed showing the running configuration.

This script is useful for performing quick configuration queries on Cisco devices in an automated manner.

2. Script to create, configure and assign IP addresses to VLANs:

from netmiko import ConnectHandler

ssh = ConnectHandler(
    device_type="cisco_ios",
    host="192.168.10.2",
    port=22,
    username="womar1",
    password="womar"
)
ssh.enable()
comandos = [
    "hostname uwu",
    "vlan 10",
    "interface vlan 10",
    "ip address 192.168.2.1 255.255.255.0",
    "no shutdown",
    'interface range fa0/1 - 5',  # Corrección aquí
    "switchport mode access",
    'switchport access vlan 10',
    "vlan 20",
    "interface vlan 20",
    "ip address 192.168.3.1 255.255.255.0",
    "no shutdown",
    'interface range fa0/6 - 10',  # Corrección aquí
    "switchport mode access",
    'switchport access vlan 20',
    "vlan 10",
    "interface vlan 30",
    "ip address 192.168.4.1 255.255.255.0",
    "no shutdown",
    'interface range fa0/11 - 15',  # Corrección aquí
    "switchport mode access",
    'switchport access vlan 30',
    "vlan 10",
    "interface vlan 40",
    "ip address 192.168.5.1 255.255.255.0",
    "no shutdown",
    'interface range fa0/16 - 20',  # Corrección aquí
    "switchport mode access",
    'switchport access vlan 40',
    "vlan 50",
    "interface vlan 50",
    "ip address 192.168.200.1 255.255.240.0",
    "no shutdown",
    'interface range fa0/21 - 22',  # Corrección aquí
    "switchport mode access",
    'switchport access vlan 50',



]
ssh.send_config_set(comandos)
configuracion = ssh.send_command("show run")
comands = ssh.find_prompt()

print(comands)
print("show run:\n" + configuracion)

Operation:

  • Connection and privileged mode: An SSH connection is established and switched to privileged mode with ssh.enable().
  • Command list: Several VLANs are created, interfaces and IP addresses are assigned to these VLANs, and the ports are configured in switchport mode access.
  • Configuration application: Commands are sent in bulk with ssh.send_config_set().
  • Configuration verification: The show run command is used to obtain the current configuration of the device.
  • Printing the result: The device prompt and the resulting configuration are printed.

This script is ideal for automating the configuration of VLANs and assigning IPs to interfaces, facilitating the administration of complex networks quickly and efficiently.

Resources needed:
Before you start automating the configuration of Cisco devices using Python, it is important to ensure that you have the right environment. Here I detail the resources and tools you will need:

1. Installing Python and Libraries

You must have Python 3.6 or higher installed on your system. If you don't have it yet, you can easily install it depending on your operating system.

To interact with Cisco devices in an automated way, we use Netmiko, a Python library that facilitates SSH connection to routers and switches.

  • Netmiko: It is the main library that we use to connect to network devices (such as routers or switches) through SSH.
  • Paramiko: Netmiko depends on this library, which is an SSH client in Python.
  • PIP: It is the Python package manager and you need it to install the libraries.

2. Installation of the Libraries

To install Netmiko and its dependencies (including Paramiko), run the following command in your terminal:

pip install netmiko

This command will download and install Netmiko along with its necessary dependencies. Once finished, you can check that everything has been installed correctly using:

pip list

This will show you all the installed libraries, among them you should see netmiko and paramiko.

3. SSH Access to Cisco Devices

In addition to the installed libraries, you need to make sure that the Cisco device (router or switch) is configured to accept SSH connections. Below are some key points to enable access:

a) Enable SSH on the Cisco device:

configure terminal
ip domain-name cisco.local
crypto key generate rsa
username cisco privilege 15 secret cisco
line vty 0 4
transport input ssh
login local
exit

b) Verify credentials and IP:

  • Make sure you have the correct credentials (username and password) and that the device's IP address is accessible from the machine where you will run the Python scripts.

With these configurations, you are ready to run scripts and automate tasks on Cisco devices using Python.

Conclusion

With the right resources (Python, Netmiko, SSH enabled on Cisco devices) and the necessary libraries installed, you will be ready to start automating the configuration and management of your network devices using Python. Netmiko makes it easy to connect and execute commands on these devices, simplifying repetitive tasks and improving efficiency in network management.

The above is the detailed content of Cisco Automation with Python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn