⚡ IT Wisdom Security told us to rotate our keys. Now nobody can open any doors.
Skilled Practitioner
4249 XP 1751 to Expert
Units
Whetting Your Appetite — Why Python for Networking? Using the Python Interpreter An Informal Introduction to Python — Network Basics Control Flow — Making Decisions in Network Scripts Data Structures — Modeling Network Inventories Modules — Organizing Your Network Scripts Input and Output — Reading and Writing Network Data Errors and Exceptions — Building Reliable Network Scripts Classes — Modeling Network Devices as Objects Standard Library Part I — Tools Every Network Script Needs Standard Library Part II — Logging, Formatting, and Counting Virtual Environments and Packages — Isolating Your Dependencies Capstone — Build a Network Inventory Tool

The Python Interpreter

When you type python3 in your terminal, you are starting the Python interpreter. The interpreter is what reads your code and executes it. Think of it like the CLI on a Cisco device — the device always had processing power, the CLI is just the interface you use to give it instructions.

Python has two modes: interactive and script. You will use both constantly.


Interactive Mode — The >>> Prompt

Type python3 with no arguments and you get the interactive prompt:

$ python3
Python 3.11.0 (default, ...)
Type "help", "copyright", "credits" or "license" for more information.
>>>

The >>> means Python is waiting for you. Type one line, press Enter, see the result immediately. This is like SSH into a device and running show commands — instant feedback, no files, no saving.

>>> hostname = "sw-core-01"
>>> hostname
'sw-core-01'
>>> len(hostname)
10
>>> type(hostname)
<class 'str'>

Interactive mode is for testing ideas quickly. When you close the session it is gone — nothing is saved. Think of it like a scratchpad.

To exit, type exit() or press Ctrl+D.


Running a Script File

For anything you want to keep, write it in a .py file and run it with python3:

python3 check_devices.py

The interpreter reads your file top to bottom and runs every line in order. This is how real automation works — write once, run whenever you need it. Think of it like a saved EEM applet: defined once, triggered on demand.


Passing Arguments to Scripts — sys.argv

When you run a script, you can pass extra information directly on the command line:

python3 check_device.py sw-core-01

Inside the script, sys.argv holds everything you typed as a list:

import sys

device = sys.argv[1]
print(f"Checking device: {device}")

Output:

Checking device: sw-core-01

sys.argv[0] is always the script name itself. sys.argv[1] is the first argument — in this case sw-core-01.

Think of it like a CLI command with a required argument: ping 192.168.1.1. The command is ping, the argument is the IP address. sys.argv is how Python receives that argument.

import sys

# sys.argv is always a list
# sys.argv[0] = 'check_device.py'  (the script name)
# sys.argv[1] = 'sw-core-01'       (what you typed after the script name)

script_name = sys.argv[0]
device      = sys.argv[1]

print(f"Script : {script_name}")
print(f"Device : {device}")

Quick Calculations in Interactive Mode

The interactive prompt is useful for quick network math — no script needed, no file to save:

>>> 2 ** 8 - 2
254
>>> 2 ** 16 - 2
65534
>>> vlans = list(range(10, 21))
>>> vlans
[10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
>>> len(vlans)
11

2 ** 8 means 2 to the power of 8 — the number of addresses in a /24. Subtract 2 for network and broadcast and you get 254 usable hosts. The interactive prompt gives you the answer in one line without opening a calculator.


Source File Encoding

Python 3 uses UTF-8 by default. You do not need to declare encoding unless you are working with a file that uses something different. If you ever see this at the top of someone else's script:

# -*- coding: utf-8 -*-

That is just an explicit declaration — not needed in Python 3 for normal work. You can ignore it safely.


Summary

  • python3 with no arguments → interactive mode — test ideas at >>>, nothing is saved
  • python3 filename.py → run a script file — use this for anything you want to keep
  • sys.argv → a list of everything you typed on the command line — [0] is the script name, [1] onwards are your arguments
  • Interactive mode is your scratchpad — scripts are your automation
  • Python 3 defaults to UTF-8 — no need to declare encoding in normal scripts