send_config_set, error_pattern, verification & save_config
Push a configuration change
Lesson 9 of 20 · 14 min · Module 3: Change the network
Reading is safe. Changing is where automation pays off, and where a mistake spreads to every device in seconds. Netmiko sends configuration lines for you and can stop at the first one the device rejects.
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…
- Send configuration lines with send_config_set
- Stop at the first rejected line
- Verify and save the change
The idea
send_config_set(lines) enters configuration mode (configure terminal on Cisco, system-view on Huawei), sends each line and leaves again. It returns the whole session as text. A device that rejects a line just prints an error, so pass error_pattern=r"% Invalid": Netmiko then raises ConfigInvalidException at the first rejected line. After the change, check it with a show command, then call save_config() (write memory on Cisco) so the change survives a reload.
lines = ["vlan 30", "name GUESTS"]
with ConnectHandler(**device) as conn:
print(conn.send_config_set(lines, error_pattern=r"% Invalid"))
print(conn.save_config())
01Configuration mode, line by line
Netmiko enters configuration mode, sends the lines and runs end. The returned text shows each prompt, so you can see where every line went.
import os
from netmiko import ConnectHandler
USERNAME = os.environ["NET_USERNAME"]
PASSWORD = os.environ["NET_PASSWORD"]
device = {"device_type": "cisco_ios", "host": "10.0.0.12",
"username": USERNAME, "password": PASSWORD}
with ConnectHandler(**device) as conn:
print(conn.send_config_set(["vlan 50", "name LAB"]))configure terminal
Enter configuration commands, one per line. End with CNTL/Z.
sw2(config)#vlan 50
sw2(config-vlan)#name LAB
sw2(config-vlan)#end
sw2#02A rejected line is only text
sw2 has no port 0/99. The switch prints an error, Netmiko returns it inside the output, and nothing stops the script.
with ConnectHandler(**device) as conn:
output = conn.send_config_set(["interface GigabitEthernet0/99"])
print(output)configure terminal
Enter configuration commands, one per line. End with CNTL/Z.
sw2(config)#interface GigabitEthernet0/99
^
% Invalid input detected at '^' marker.
sw2(config)#end
sw2#03error_pattern makes it stop
With error_pattern, Netmiko searches each answer for the pattern and raises ConfigInvalidException naming the line. Your script can then stop before the next device.
from netmiko import ConfigInvalidException
try:
with ConnectHandler(**device) as conn:
conn.send_config_set(["interface GigabitEthernet0/99"],
error_pattern=r"% Invalid")
except ConfigInvalidException as error:
print("stopped:", error)stopped: Invalid input detected at command: interface GigabitEthernet0/99Your turn
sw1 (10.0.0.11), create VLAN 30 named GUESTS and put the unused port GigabitEthernet0/6 into it as an access port with the description Guest kiosk. Send the lines with error_pattern, check the result with show vlan brief, and save the configuration.What the checks look for
- VLAN 30 GUESTS exists on sw1
- Gi0/6 is an access port in VLAN 30
- Gi0/6 has the description Guest kiosk
- Checked the VLANs with a show command
- The configuration is saved
- Sent the lines with error_pattern
Take this with you
Send, verify, save: send_config_set with error_pattern, a show command, then save_config.
Go a little further
Put the lines in a file named guest.txt with open(..., "w") and send them with conn.send_config_from_file("guest.txt", error_pattern=r"% Invalid").