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:
- 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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Python is suitable for data science, web development and automation tasks, while C is suitable for system programming, game development and embedded systems. Python is known for its simplicity and powerful ecosystem, while C is known for its high performance and underlying control capabilities.

You can learn the basics of Python within two hours. 1. Learn variables and data types, 2. Master control structures such as if statements and loops, 3. Understand the definition and use of functions. These will help you start writing simple Python programs.

Python excels in gaming and GUI development. 1) Game development uses Pygame, providing drawing, audio and other functions, which are suitable for creating 2D games. 2) GUI development can choose Tkinter or PyQt. Tkinter is simple and easy to use, PyQt has rich functions and is suitable for professional development.

You can learn basic programming concepts and skills of Python within 2 hours. 1. Learn variables and data types, 2. Master control flow (conditional statements and loops), 3. Understand the definition and use of functions, 4. Quickly get started with Python programming through simple examples and code snippets.

Python is widely used in the fields of web development, data science, machine learning, automation and scripting. 1) In web development, Django and Flask frameworks simplify the development process. 2) In the fields of data science and machine learning, NumPy, Pandas, Scikit-learn and TensorFlow libraries provide strong support. 3) In terms of automation and scripting, Python is suitable for tasks such as automated testing and system management.

Python is easier to learn and use, while C is more powerful but complex. 1. Python syntax is concise and suitable for beginners. Dynamic typing and automatic memory management make it easy to use, but may cause runtime errors. 2.C provides low-level control and advanced features, suitable for high-performance applications, but has a high learning threshold and requires manual memory and type safety management.

To maximize the efficiency of learning Python in a limited time, you can use Python's datetime, time, and schedule modules. 1. The datetime module is used to record and plan learning time. 2. The time module helps to set study and rest time. 3. The schedule module automatically arranges weekly learning tasks.

Python excels in automation, scripting, and task management. 1) Automation: File backup is realized through standard libraries such as os and shutil. 2) Script writing: Use the psutil library to monitor system resources. 3) Task management: Use the schedule library to schedule tasks. Python's ease of use and rich library support makes it the preferred tool in these areas.
