Home > Article > Backend Development > Cisco Automation with 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:
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:
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.
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:
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!