Post

Automating EVE-NG Topology and startup configs

In the world of network engineering, setting up labs to simulate network environments is crucial for testing and experimentation. EVE-NG (Emulated Virtual Environment-Next Generation) is a popular tool that allows engineers to emulate complex network topologies. However, manually configuring each device, setting up connections, and assigning IP addresses can be time-consuming. What if you could automate this entire process with a simple Python script? In this blog, we’ll explore a Python script that does just that—automating the creation of a .unl file for EVE-NG, complete with point to point connections, and startup configurations.

Code

Checkout the entire github project at eve-ng-topology-generator

The Big Picture: What Does the Script Do?

This Python script is designed to automate the generation of a .unl file, which EVE-NG uses to define network topologies. It also generates a YAML file that summarizes the network setup. Here’s a high-level overview of the process:

  1. User Inputs: The script collects various inputs from the user, such as router names, the number of routers, connections between them, and IP prefix for subnet allocation.
  2. Subnet Generation: Based on the inputted IP prefix, the script generates subnets for each router connection. EVE-NG File Creation: The script builds an XML structure that defines the lab topology in EVE-NG. This includes creating nodes (routers), interfaces, and networks.
  3. Configuration Templates: Using Jinja2 templates, the script generates startup configurations for each router, which are then encoded and embedded in the .unl file.
  4. File Output: Finally, the script outputs the .unl and YAML files, ready to be used in EVE-NG.

Breaking Down the Script: How It Works

Let’s dive into the script, step by step.

Setting the Stage: Imports and Libraries

At the beginning of the script, various Python libraries are imported:

  • Standard Libraries: os, re, yaml, uuid, base64, xml.etree.ElementTree, minidom.
  • Network-Specific Libraries: ipaddress, netaddr.
  • Templating: jinja2.

These libraries handle everything from file operations to IP management, XML manipulation, and configuration templating.

Gathering User Input

The script is designed to be interactive, asking the user for several inputs:

  • Router Info: The get_router_info() function collects the router name prefix and the number of routers. It returns a list of router names (e.g., R1, R2, etc.).
  • Connections: With get_connections(routers), the script asks the user if each pair of routers should be connected, building a list of connections (e.g., [(R1, R2), (R1, R3)]).
  • IP Prefix: The get_prefix() function asks for an IP prefix (e.g., 10.0.0.0/16). This prefix will be used to generate subnets for the router connections.
  • Lab Name, Template, and Image: The functions get_lab_name(), get_eve_ng_template(), and get_eve_ng_image() collect additional details like the lab’s name, the EVE-NG template (e.g., iol), and the router image to use.

Subnet Generation

Once the user inputs are gathered, the script generates subnets for each router connection:

  • generate_subnets(prefix, connections): This function takes the IP prefix and the list of connections. It generates a unique /24 subnet for each connection using Python’s ipaddress module. These subnets are stored in a dictionary, mapping each connection to its corresponding subnet.

Creating the .unl File

This is where the magic happens—turning the collected data into an EVE-NG-compatible .unl file:

  • Template Loading: The load_template(template_name) function loads a YAML template for the specific router type. This template contains configuration details like memory size, icon, and the number of Ethernet ports.

Building the XML Structure:

  • The generate_unl_file() function creates the base XML structure that defines the lab. This includes the element and its various attributes like name, id, and version.
  • Nodes (Routers): The script iterates over the list of routers, creating an XML element for each one. Each node is assigned attributes based on the loaded template.
  • Interfaces: For each router, the script creates interfaces, connecting them to other routers as specified by the user. If the iol template is used, the script ensures that Ethernet slots are allocated correctly.
  • Networks: The script creates network elements in the XML that represent the connections between routers. Each network is labeled with its corresponding subnet.

Generating Startup Configurations

One of the coolest features of this script is its ability to automatically generate startup configurations for each router:

  • Jinja2 Templating: The script uses Jinja2 to load a configuration template (iol_config_template.j2) for each router. It fills in details like the router hostname and IP addresses for each interface.
  • Base64 Encoding: The generated configuration is then encoded in Base64, as required by EVE-NG, and inserted into the .unl file.

Outputting the Files

After building the XML structure and generating the configurations, the script outputs two files:

  • .unl File: This file is the heart of your EVE-NG lab. It contains the entire topology, with nodes, connections, and startup configurations.
  • YAML File: This file provides a summary of the lab, listing each router and its interfaces with the corresponding subnets. (At a later date I will use the YAML to import this data into a source of truth like nautobot. )

Conclusion: Automate Your Network Labs

By automating the creation of EVE-NG labs, this script can save you a ton of time and effort. Whether you’re testing a new network design, preparing for a certification exam, or simply experimenting with configurations, this script lets you focus on what really matters—understanding and refining your network. With just a few inputs, you could generate a fully configured lab environment, ready to be deployed in EVE-NG. Happy networking!

Join the Conversation

Links

Go and checkout what EVE-NG is doing at https://www.eve-ng.net/

This post is licensed under CC BY 4.0 by the author.