Foundation project
Project: standardize NTP and logging
Lesson 12 of 20 · 25 min · Module 3: Change the network
Your first automation project. Every switch must use the same two time servers and the same log server, so that logs from different devices line up during an outage. Find the devices that drift from the standard and fix only them.
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…
- Read only the lines you need with a filtered show command
- Turn two set differences into commands
- Use one plan for two vendors
The idea
The standard: NTP servers 10.0.0.250 and 10.0.0.251, and exactly one log server, 10.0.0.50. On Cisco the lines are ntp server A and logging host A, removed with no; on Huawei they are ntp-service unicast-server A and info-center loghost A, removed with undo. Read the current lines with a filtered show command, compare the addresses as sets, and build the commands from the two differences: missing servers to add, extra servers to remove.
show = "show running-config | include ntp server|logging host"
lines = ["ntp server 10.0.0.250", "logging host 10.0.0.99"]
print({l.split()[-1] for l in lines if l.startswith("ntp server ")})
01A filtered show command
The include filter takes a regular expression, so ntp server|logging host keeps the lines that contain either text.
import os
from netmiko import ConnectHandler
USERNAME = os.environ["NET_USERNAME"]
PASSWORD = os.environ["NET_PASSWORD"]
with ConnectHandler(device_type="cisco_ios", host="10.0.0.12",
username=USERNAME, password=PASSWORD) as conn:
print(conn.send_command(
"show running-config | include ntp server|logging host"))logging host 10.0.0.99
ntp server 10.0.0.250
ntp server 10.0.0.25102Two differences, two kinds of command
wanted - current is what to add; current - wanted is what to remove. Sorting keeps the commands in a stable order.
current = {"10.0.0.99"}
wanted = {"10.0.0.50"}
print([f"no logging host {a}" for a in sorted(current - wanted)])
print([f"logging host {a}" for a in sorted(wanted - current)])['no logging host 10.0.0.99']
['logging host 10.0.0.50']03The Huawei side
Huawei uses other words for the same settings. Keep them in a dictionary per platform, and the plan stays the same.
with ConnectHandler(device_type="huawei", host="10.0.0.13",
username=USERNAME, password=PASSWORD) as conn:
print(conn.send_command(
"display current-configuration | include ntp-service|loghost"))info-center loghost 10.0.0.50Your project brief
devices.csv: read its NTP and log servers, work out the commands that bring it to the standard, send them only if there are any, and save. Build report, keyed by device name: the list of commands you sent (an empty list for a device that already follows the standard), or the string "unreachable" when the device does not answer.What the checks look for
- Every device is in the report
- Compliant devices need no commands
- sw1 gets its missing NTP server
- sw2 swaps its log server
- sw3 gets both NTP servers with Huawei commands
- sw4 is reported as unreachable
- Every reachable device now follows the standard
- Changed devices are saved, compliant ones untouched
Take this with you
Compare parsed values with the standard and send only the commands that close the gap.
Go a little further
Add a DRY_RUN = True switch: build the same report, but print the commands instead of sending them.