Part 4: Connecting to the nodes in a lab

containerlab
workshop
Reach lab nodes by SSH, docker exec and containerlab exec; connect from another machine with a jump host, a route or a published port; and drive the lab from Python with the inventory Containerlab generates.
Author

Abid Ghufran

Published

November 7, 2026

A lab is only useful once you can get into its nodes. Containerlab gives you several ways in, from a shell on the lab host to a Python script that logs in to every router. This part tries each one on the routed lab from Part 3, then verifies them all.

Method Works from Best for
SSH by node name the lab host everyday work on one router
docker exec the lab host Linux hosts, and routers whose login you have lost
containerlab exec the lab host one command on many nodes at once
SSH through a jump host any machine that can SSH to the lab host working from your laptop
A route to the management network a machine on the same LAN using desktop tools such as SecureCRT or Wireshark directly
A published port any machine that can reach the lab host one service on one node
Generated inventories the lab host Ansible, Nornir and Python automation

Start the lab

Update your copy of the workshop files, then deploy the Part 3 lab:

cd ~/dynanet-dev && git pull
cd ~/dynanet-dev/clab-workshop/labs/03-topology
containerlab deploy -t routed.clab.yml

To recall the lab’s layout, open it in the topology viewer. The commands below assume you are in ~/dynanet-dev/clab-workshop/labs/03-topology unless they say otherwise.

SSH by node name

Containerlab adds every node to the lab host’s /etc/hosts as clab-<lab>-<node>, so you can use names instead of addresses:

ssh clab-routed-xrd1

SSH asks for the password, clab@123, and you land at the IOS XR prompt, RP/0/RP0/CPU0:xrd1#. Leave it with exit.

You did not have to type a username, because Containerlab writes /etc/ssh/ssh_config.d/clab-routed.conf at deploy time. That file sets the username for each node (clab for XRd) and turns off host key checks for the lab’s nodes, so a redeployed node does not trigger a warning.

XRd takes its users from the router’s own configuration, so you log in with the password from the startup config. Containerlab does not copy your SSH keys onto XRd nodes.

Logins for the kinds used in this workshop:

Kind Username Password
Cisco XRd clab clab@123
Arista cEOS admin admin
Linux (Alpine) no SSH server; use docker exec

docker exec

Every node is a container, so docker exec runs a program inside it. Add -it for an interactive session:

Node Command You get
Linux host docker exec -it clab-routed-h1 sh a shell (use bash on images that have it)
Cisco XRd docker exec -it clab-routed-xrd1 /pkg/bin/xr_cli.sh the IOS XR CLI, after you log in as clab
Arista cEOS docker exec -it <node> Cli the EOS CLI

In these labs the XRd startup configurations define the user clab, so xr_cli.sh asks you to log in with clab / clab@123. Containerlab’s XRd page shows the same command without a password; which of the two you get depends on the node’s startup configuration.

Without -it you can run a single command and get its output, which is handy in scripts:

docker exec clab-routed-h1 ip route
docker exec clab-routed-xrd1 /pkg/bin/xr_cli "show version"

On XRd, /pkg/bin/xr_cli runs one IOS XR command without a login. Give the full path, because the program is not on the container’s search path.

containerlab exec: one command on many nodes

containerlab exec runs a command on every node in a lab, or on the nodes that match a label. Containerlab labels each node with its name (clab-node-name) and kind (clab-node-kind):

# both routers
containerlab exec -t routed.clab.yml --label clab-node-kind=cisco_xrd --cmd '/pkg/bin/xr_cli "show version"'

# one host
containerlab exec -t routed.clab.yml --label clab-node-name=h1 --cmd "ip -4 addr show eth1"

The output is grouped by node.

Connect from another machine

The names in /etc/hosts and the SSH settings only exist on the lab host. From your laptop you have three options.

Through a jump host (works anywhere)

SSH can hop through the lab host to a node in one command. The node’s name is looked up on the lab host, so names still work:

ssh -J you@labhost clab@clab-routed-xrd1

Replace you@labhost with your login on the lab host. You are asked for the lab host password, then the router password. This works from Linux, macOS and Windows 10 or later (OpenSSH is built in), and only needs SSH access to the lab host. To make it permanent, add this to ~/.ssh/config on your laptop:

Host clab-*
    ProxyJump you@labhost

Then ssh clab@clab-routed-xrd1 works directly from the laptop.

A route to the management network (same LAN)

Three networks are involved: the LAN your laptop and the lab host share, the lab host itself, and the management network behind it.

Your laptop Lab host h1 h2 192.168.0.x 192.168.0.201 (and the clab bridge) LAN 192.168.0.0/24 routes to 172.20.20.0/24 xrd1 .11 xrd2 .12 h1 .21 h2 .22 Management network 172.20.20.0/24
The route you add on the laptop sends traffic for 172.20.20.0/24 to the lab host, which forwards it onto the management network. The numbers after the node names are the last part of each management address.

Containerlab lets other machines reach the management network through the lab host. Add a route on your laptop, replacing 192.168.0.201 with the lab host’s address:

# Ubuntu (lasts until reboot)
sudo ip route add 172.20.20.0/24 via 192.168.0.201
# Windows, in an administrator PowerShell (-p keeps it after a reboot)
route -p add 172.20.20.0 mask 255.255.255.0 192.168.0.201

Then connect to the node’s management address, for example ssh [email protected], or open a SecureCRT session to it. Use addresses rather than names, since the names only exist on the lab host. The route only works when the laptop and the lab host are on the same network; across the internet, use the jump host. After a redeploy, a node has a new host key, so remove the old one with ssh-keygen -R 172.20.20.11 if SSH warns about it.

The router can answer your laptop because of the default route in its startup config, which points out of the management interface (see Part 3). An XRd node without such a route only answers the lab host.

A published port

A node can publish one of its ports on the lab host, like docker run -p. Add ports to the node in the topology file:

    xrd1:
      kind: cisco_xrd
      ports:
        - 2201:22

After a redeploy, ssh -p 2201 clab@labhost reaches xrd1’s SSH server. This suits one service on one node; for many nodes, the jump host is simpler.

2201 22 Your laptop Lab host anywhere on the LAN 192.168.0.201 ssh -p 2201 clab@labhost forwarded to xrd1 xrd1 172.20.20.11
The lab host listens on port 2201 and passes the connection to port 22 inside xrd1. The other nodes are not reachable this way unless you publish ports for them too.

Use the generated inventories

Containerlab writes inventory files into the lab folder (clab-routed/) on every deploy:

File Used by
ansible-inventory.yml Ansible: nodes grouped by kind, with addresses and the kind’s login
nornir-simple-inventory.yml Nornir’s SimpleInventory
topology-data.json your own scripts: every node’s name, kind, image and management address, and every link

Because they are rebuilt on each deploy, they always match the running lab. Here is the main part of show_version.py from labs/04-connect, which reads topology-data.json and logs in to every XRd router:

import json
import sys

from netmiko import ConnectHandler  # pip install netmiko

data_file = sys.argv[1] if len(sys.argv) > 1 else "../03-topology/clab-routed/topology-data.json"
with open(data_file) as f:
    nodes = json.load(f)["nodes"]

for name, node in sorted(nodes.items()):
    if node["kind"] != "cisco_xrd":
        continue
    conn = ConnectHandler(
        device_type="cisco_xr",
        host=node["mgmt-ipv4-address"],
        username="clab",
        password="clab@123",
    )
    output = conn.send_command("show version")
    conn.disconnect()
    marker = "Cisco IOS XR Software, Version "
    version = next((line[len(marker):].strip() for line in output.splitlines()
                    if line.startswith(marker)), "unknown")
    print(f"{name:6} {node['mgmt-ipv4-address']:15} IOS XR {version}")

Ubuntu does not let pip install into the system Python, so install Netmiko in a virtual environment once:

sudo apt install -y python3-venv
python3 -m venv ~/clab-venv
~/clab-venv/bin/pip install netmiko

Then run the script:

cd ../04-connect
~/clab-venv/bin/python show_version.py
xrd1   172.20.20.11    IOS XR 7.11.2 LNT
xrd2   172.20.20.12    IOS XR 7.11.2 LNT

Verify

Run each check on the lab host.

Step Command You should see
1. Names grep clab-routed /etc/hosts a line for each of clab-routed-xrd1, -xrd2, -h1 and -h2 with its 172.20.20.x address (plus a second line each with an IPv6 address on some hosts)
2. SSH by name ssh clab-routed-xrd1 (then the same with xrd2), then show version, then exit a password prompt, the IOS XR prompt, then Cisco IOS XR Software, Version 7.11.2
3. docker exec docker exec clab-routed-h1 hostname h1
3a. Shell on a host docker exec -it clab-routed-h1 sh, then hostname, then exit the prompt / # and h1
3b. CLI on a router docker exec -it clab-routed-xrd1 /pkg/bin/xr_cli.sh, log in as clab / clab@123, then show version, then exit the IOS XR prompt, then Cisco IOS XR Software, Version 7.11.2
4. containerlab exec containerlab exec -t routed.clab.yml --label clab-node-kind=cisco_xrd --cmd '/pkg/bin/xr_cli "show version"' a Cisco IOS XR Software line for each of xrd1 and xrd2
5. Inventories ls clab-routed/ ansible-inventory.yml, nornir-simple-inventory.yml and topology-data.json among the files
6. Python ~/clab-venv/bin/python ../04-connect/show_version.py clab-routed/topology-data.json one line each for xrd1 and xrd2 with the IOS XR version
7. Management network from inside a node docker exec clab-routed-h1 ping -c 3 172.20.20.11 0% packet loss: h1 reaches xrd1’s management address from inside the lab
8. From your laptop ssh -J you@labhost clab@clab-routed-xrd1 the IOS XR prompt after both passwords

Or run steps 1 to 7 at once from labs/04-connect. PYTHON tells the script which Python has Netmiko:

cd ../04-connect
PYTHON=~/clab-venv/bin/python ./verify.sh
Checking connection methods on lab routed
  PASS  /etc/hosts has entries for all 4 nodes
  PASS  docker exec opens a shell on h1
  PASS  docker exec runs the IOS XR CLI on xrd1
  PASS  containerlab exec ran on both routers
  PASS  h1 reaches xrd1's management address 172.20.20.11 from inside the lab
  PASS  ssh clab-routed-xrd1 logs in as clab with the lab password
  PASS  ssh clab-routed-xrd2 logs in as clab with the lab password
  PASS  lab folder has ansible-inventory.yml
  PASS  lab folder has nornir-simple-inventory.yml
  PASS  lab folder has topology-data.json
  PASS  show_version.py reached both routers

All checks passed.

The script types the lab password for you in the SSH checks. SKIP means a check could not run on your setup, such as having no Netmiko; it is not a failure. Step 8 runs on your laptop, so the script cannot check it.

Remove the lab

cd ~/dynanet-dev/clab-workshop/labs/03-topology
containerlab destroy -t routed.clab.yml --cleanup

Troubleshooting

Symptom Fix
ssh clab-routed-xrd1 answers Connection refused or Connection timed out The router is still starting. Wait a minute and try again
SSH answers Connection closed, or Netmiko reports Error reading SSH protocol banner IOS XR limits how fast new SSH sessions may start on one router. Wait two seconds and try again
SSH from the laptop reaches the lab host but the router never answers The router has no route back to your laptop. Check the default route with show route static on the router
Could not resolve hostname clab-routed-xrd1 on your laptop Names only exist on the lab host. Use the jump host, or the management address with a route
SSH from the laptop to 172.20.20.x times out Check the route (ip route or route print), that the laptop is on the lab host’s LAN, and that the lab is running
WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED The node was redeployed and has a new key. Run ssh-keygen -R <address> and connect again
docker exec ... xr_cli.sh prints a terminal error Add -it; the IOS XR login needs an interactive terminal
ModuleNotFoundError: No module named 'netmiko' Run the script with the virtual environment’s Python: ~/clab-venv/bin/python

Next

Part 5 spreads a lab across several hosts, joined by VXLAN tunnels: first on one machine, then on separate machines or cloud VMs.

Sources: containerlab exec · Network wiring and external access · Node properties · Inventory

Comments

Comments are reviewed before they appear.