Netmiko, ConnectHandler, send_command & credentials from the environment
Run a command on a switch
Lesson 1 of 20 · 12 min · Module 1: First steps with automation
Network automation starts small: log in to a switch from Python, run a show command and read the answer. The Netmiko library does the SSH part for you, on Cisco, Huawei and many other vendors.
Start this lesson →Free. No installation: you write and run the code in your browser, and it is checked automatically.
By the end, you’ll be able to…
- Describe a device with a Netmiko dictionary
- Run a show command and keep its output
- Keep credentials out of your code
The idea
Netmiko describes a device with a dictionary: device_type picks the driver (cisco_ios for Cisco IOS, huawei for Huawei VRP), then host, username and password. ConnectHandler(**device) logs in and returns a connection, and send_command() returns everything the device printed as one string. A with block closes the session for you. Passwords never go in the code: read them from environment variables with os.environ.
Every exercise in this course runs against a practice network inside your browser: a core switch and access switches in Bucharest and Cluj, running simulated Cisco IOS and Huawei VRP. Nothing you send reaches a real device, so experiment freely. The code you write here works with the real Netmiko after pip install netmiko.
import os
from netmiko import ConnectHandler
device = {
"device_type": "cisco_ios",
"host": "10.0.0.11",
"username": os.environ["NET_USERNAME"],
"password": os.environ["NET_PASSWORD"],
}
with ConnectHandler(**device) as conn:
print(conn.find_prompt())
01Credentials come from the environment
The practice lab sets NET_USERNAME and NET_PASSWORD for you, the way a CI job or a vault would on a real team. os.environ reads them like a dictionary.
import os
print(os.environ["NET_USERNAME"])
print("NET_PASSWORD" in os.environ)netadmin
True02Log in and look at the prompt
ConnectHandler(**device) unpacks the dictionary into keyword arguments and logs in. find_prompt() returns the prompt; # means privileged mode on Cisco.
from netmiko import ConnectHandler
device = {
"device_type": "cisco_ios",
"host": "10.0.0.11",
"username": os.environ["NET_USERNAME"],
"password": os.environ["NET_PASSWORD"],
}
with ConnectHandler(**device) as conn:
print(conn.find_prompt())sw1#03A show command returns text
send_command() waits for the prompt to come back and returns what the device printed, without the command and the prompt. It is a plain string: printing it shows the table, and Python can search or split it.
with ConnectHandler(**device) as conn:
clock = conn.send_command("show clock")
print(type(clock).__name__)
print(clock)str
09:00:05.785 UTC Mon Oct 19 2026Your turn
core1 at 10.0.0.1 (Cisco IOS). Store the output of show ip interface brief in a variable named output and print it. Read the username and password from the NET_USERNAME and NET_PASSWORD environment variables.What the checks look for
- output holds the interfaces of core1
- The output is printed
- Logged in to core1
- The password is not written in the code
Take this with you
ConnectHandler(**device) logs in; send_command() returns the output as one string.
Go a little further
Also print conn.find_prompt() and only the first line of show version (output.splitlines()[0]).