IxNetwork Python Client

1.9.0 · active · verified Thu Apr 16

The ixnetwork-restpy library is the official Python client for automating Keysight's IxNetwork via its REST API. It provides an object-oriented interface to programmatically control test platforms, configure network topologies, generate traffic, and collect statistics. The library is actively maintained, with frequent minor releases (typically monthly or bi-monthly) to support new IxNetwork features and address bug fixes, currently at version 1.9.0.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to connect to an IxNetwork API server, authenticate, map physical ports, create a basic traffic item, start/stop traffic, and retrieve statistics. Replace '10.0.0.1' with your IxNetwork chassis IP, and ensure the `IXNET_SERVER_IP`, `IXNET_USERNAME`, and `IXNET_PASSWORD` environment variables are set or updated in the script. It uses `SessionAssistant` for simplified workflow and `PortMapAssistant` for port management.

import os
from ixnetwork_restpy import SessionAssistant, Files

# Configure connection parameters
IXNETWORK_API_SERVER_IP = os.environ.get('IXNET_SERVER_IP', '127.0.0.1')
IXNETWORK_USERNAME = os.environ.get('IXNET_USERNAME', 'admin')
IXNETWORK_PASSWORD = os.environ.get('IXNET_PASSWORD', 'admin')

# Create a session and connect to IxNetwork
try:
    session_assistant = SessionAssistant(
        IpAddress=IXNETWORK_API_SERVER_IP,
        UserName=IXNETWORK_USERNAME,
        Password=IXNETWORK_PASSWORD,
        ClearConfig=True, # Clears any existing configuration on the server
        LogLevel=SessionAssistant.LOGLEVEL_INFO
    )
    ixnetwork = session_assistant.Ixnetwork
    print(f"Connected to IxNetwork API Server at {IXNETWORK_API_SERVER_IP}")

    # Example: Create a simple configuration (e.g., two ports, basic traffic)
    # These IPs and ports must match your physical setup
    port_map = session_assistant.PortMapAssistant()
    port_map.Map('10.0.0.1', 1, 1, Name='TxPort') # Chassis IP, Card ID, Port ID
    port_map.Map('10.0.0.1', 1, 2, Name='RxPort') # Chassis IP, Card ID, Port ID
    port_map.Connect(ForceOwnership=True)
    print("Ports mapped and connected.")

    # Add a traffic item
    traffic_item = ixnetwork.Traffic.TrafficItem.add(Name='Simple Traffic', TrafficType='raw')
    traffic_item.EndpointSet.add(
        Sources=ixnetwork.Vport.find(Name='^TxPort$').Protocols.find(),
        Destinations=ixnetwork.Vport.find(Name='^RxPort$').Protocols.find()
    )
    traffic_config = traffic_item.ConfigElement.find()
    traffic_config.FrameRate.update(Type='percentLineRate', Rate='10')
    traffic_config.TransmissionControl.update(Type='continuous')
    print("Traffic item configured.")

    # Apply and start traffic
    ixnetwork.Traffic.Apply()
    ixnetwork.Traffic.StartStatelessTrafficBlocking()
    print("Traffic started. Waiting for stats...")

    # Get and print statistics
    port_stats = session_assistant.StatViewAssistant('Port Statistics')
    traffic_item_stats = session_assistant.StatViewAssistant('Traffic Item Statistics')
    
    print("\n--- Port Statistics ---")
    print(port_stats)
    print("\n--- Traffic Item Statistics ---")
    print(traffic_item_stats)

    # Stop traffic and clear config
    ixnetwork.Traffic.StopStatelessTrafficBlocking()
    session_assistant.ClearConfig()
    print("Traffic stopped and configuration cleared.")

except Exception as e:
    print(f"An error occurred: {e}")
finally:
    if 'session_assistant' in locals() and session_assistant.Is</span>Connected:
        # Ensure the session is properly closed if it was opened
        session_assistant.Session.delete()
        print("Session deleted.")

view raw JSON →