Environment checks, sys.exit, re.sub & redacting configurations

Keep secrets out of code and backups

Lesson 18 of 20 · 15 min · Module 5: APIs and the source of truth

Scripts need passwords to log in, and backups contain password hashes and SNMP communities. Keep secrets out of the code, out of the output, and out of the files you share.

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…

  • Stop clearly when a secret is missing
  • Replace secrets with re.sub and groups
  • Keep secrets out of output and files

The idea

os.environ.get("NET_PASSWORD") returns None when the variable is missing, so the script can stop with a clear message: sys.exit("...") ends it and prints the text, instead of a long traceback. Before a configuration leaves the script, redact it: re.sub(pattern, replacement, text) replaces every match, and \1 in the replacement puts back the first group, so re.sub(r"(secret) \d+ \S+", r"\1 <removed>", line) keeps the keyword and drops the hash.

import re

line = "enable secret 9 $9$Y9GywvSYJdC3fz$Kt4NQ6LQgXGiAWSq"
print(re.sub(r"(secret) \d+ \S+", r"\1 <removed>", line))

01get() instead of []

os.environ["X"] raises KeyError for a missing variable; os.environ.get("X") returns None, which the script can check.

import os

print(os.environ.get("NET_TOKEN"))
print(os.environ.get("NET_USERNAME"))
Output
None
netadmin

02sys.exit with a message

sys.exit raises SystemExit. In a terminal, Python prints the message and ends with exit status 1, which tells a scheduler or a CI job that the run failed.

import sys

try:
    sys.exit("NET_TOKEN is not set. Export it before running this script.")
except SystemExit as stop:
    print("the script would stop here:", stop.code)
Output
the script would stop here: NET_TOKEN is not set. Export it before running this script.

03Keep the keyword, drop the value

The group (secret) is put back with \1, and \d+ \S+ swallows the type number and the hash.

import re

line = "username netadmin privilege 15 secret 9 $9$QLcD05Vb$IkXHAdNm"
print(re.sub(r"(secret) \d+ \S+", r"\1 <removed>", line))
line = "snmp-server community public RO"
print(re.sub(r"(snmp-server community) \S+", r"\1 <removed>", line))
Output
username netadmin privilege 15 secret <removed>
snmp-server community <removed> RO

Your turn

Write credentials(), which returns (username, password) from NET_USERNAME and NET_PASSWORD and stops the script with sys.exit and a message naming the missing variable. Write redact(config), which replaces Cisco secret <type> <hash> with secret <removed>, the SNMP community in snmp-server community <name> with <removed>, and Huawei irreversible-cipher <hash> with irreversible-cipher <removed>. Save redacted backups of sw1 and sw3 to backups/sw1.cfg and backups/sw3.cfg.

What the checks look for

  • credentials() returns the username and password
  • credentials() stops with a message naming NET_PASSWORD
  • redact removes Cisco secrets and SNMP communities
  • redact removes Huawei password hashes
  • redact leaves other lines alone
  • The backups are redacted
  • No secret in the code or the output
Try it in the editor →

Take this with you

Secrets come from the environment, never from the code, and are removed before a configuration leaves the script.

Go a little further

Also redact older lines such as username admin password 7 0822455D0A16, with a fourth pattern for password 7.

Learn Network Automation for free

20 short lessons with real code and instant checks.

See the whole course →