This script is used to verify VLAN configurations on Cisco NX-OS Switches using the Cisco PyATS and Genie framework. It connects to network devices defined in a testbed file, retrieves VLAN information, and ensures that the expected VLANs are configured correctly.
The script contains setup, test, and cleanup sections to automate the process of verifying VLAN configurations, making it efficient and reliable for network validation.
- Python 3.x installed.
- Cisco PyATS and Genie libraries installed.
- A testbed YAML file (
testbed.yaml
) that defines the network devices to be tested. - Proper access to the devices defined in the testbed.
To install the necessary Python libraries, run:
pip install pyats genie
- Clone the repo
git clone https://github.com/emomeni/pyats_vlan_verification.git
- Go to your project directory
cd pyats_vlan_verification
- Set up a Python virtual environment First make sure that you have Python 3 installed on your machine. We will then be using venv to create an isolated environment with only the necessary packages.
3.1. Install virtualenv via pip
pip install virtualenv
3.2. Create the venv
python3 -m venv venv
3.3. Activate your venv
source venv/bin/activate
- Install dependencies
pip install -r requirements.txt
In the following, we have different sections of the code.
-
CommonSetup Class: Connects to all devices in the testbed before running the test cases.
- The connect_to_devices method attempts to connect to each device and logs the connection status.
- Connects to all devices defined in the testbed YAML file.
- Stores the connected devices in self.parent.parameters['devices'].
- Logs a message upon a successful connection.
- Fails the script if it cannot connect to a device.
-
VLANVerificationTestcase Class: Contains the setup, test, and cleanup methods for verifying VLAN configurations.
-
Setup: Connects to a specific device (leaf1) and loads VLAN information.
- Attempts to get a specific device (leaf1) from the testbed.
- If the device is not found, it fails the setup.
- Runs the show vlan command to get VLAN data and parses the output.
- If parsing fails, the setup is marked as failed.
-
Test (verify_vlans): Verifies if all the expected VLANs are present on the device. If any VLANs are missing, the test fails.
- Verifies that all expected VLANs (EXPECTED_VLANS) are present in the device's VLAN configuration.
- Converts VLAN IDs from the parsed output to integers and stores them in vlan_list.
- If any expected VLAN is missing, it adds it to missing_vlans.
- Uses an assert statement to raise an error if there are any missing VLANs.
-
Cleanup: Disconnects from the device after verification.
- Disconnects from leaf1.
- Logs a message to confirm disconnection.
- Logs a warning if disconnection fails.
-
-
CommonCleanup Class: Disconnects from all devices connected during the test.
- Disconnects from all devices connected during CommonSetup.
- Logs a message to indicate that each device has been disconnected.
- Logs a warning if it cannot disconnect from a device.
- You can provide the list of VLANs to verify as command-line arguments.
- If no arguments are provided, the default VLANs
[11, 12, 13]
will be used.
- Ensure you have a valid
testbed.yaml
file that defines your network devices. - Run the script as follows:
python vlan_verification.py [vlan_id1 vlan_id2 ...]
For example, to verify VLANs 20, 30, and 40:
python vlan_verification.py 20 30 40
- The script logs the connection status to each device, the verification of VLANs, and the cleanup process.
- If any VLANs are missing, it raises an assertion error indicating which VLANs were not found.
- The script uses Python's logging library to log information, warnings, and errors.
- Log messages are printed to the console, providing real-time updates on the script's progress.
- The script ensures that all devices are properly disconnected at the end of the test, either through the test case cleanup or the common cleanup section.
- Connection Errors: Ensure that the devices are accessible over the network and that credentials are correct in the
testbed.yaml
file. - Missing VLANs: If the script reports missing VLANs, verify the network configuration manually to confirm if the VLANs are properly configured.
This script is a useful tool for automating VLAN configuration checks on network devices. By leveraging PyATS and Genie, it provides a robust way to validate network configurations, helping network engineers ensure that their network is set up correctly.
This project is licensed under the MIT License.