Python for Network Automation - Part 2 (Foundation)


Welcome to the foundational segment of our series on "Python for Network Automation". Before we dive into the advanced intricacies of automating network tasks with Python, it's crucial to have a solid understanding of the basics.

In this entry, we'll explore the fundamental Python concepts tailored for network engineers. Through real-world examples, we'll demonstrate how even basic Python knowledge can significantly enhance your network automation capabilities.

Let's embark on this foundational journey, setting the stage for more advanced topics in subsequent parts!


Python, with its simplicity and extensive libraries, has become an indispensable tool for network engineers. This article will guide you through some fundamental Python concepts, tailored for network professionals.

Printing in Python:

In Python, the `print` function is used to display output. Depending on the Python version, the syntax can vary:

# Python 2.7 and below
print "Hello, Network World using 'old Python'!"

# Python 3.x and above
print("Hello, Network World using 'new Python'!")

Data Types and Variables

Python supports various data types, but for network engineers, strings and integers are most commonly used. Remember, you cannot concatenate a string and an integer directly. However, you can convert between these types:

ip_address = ""
port = 8080

# Correct way to concatenate
print("IP Address: " + ip_address + ", Port: " + str(port))


Lists and Tuples

Lists and tuples are essential data structures in Python:

- List: A dynamic collection of items. Useful for tasks like storing IP addresses or device names.

devices = ["Router1", "Switch2", "Firewall1"]
devices.append("Router2")  # Adding another device


- Tuple: A fixed collection of items. Ideal for storing constant data, like a device's credentials.

 credentials = ("admin", "password123")
Note for Network Engineers: Lists are mutable, meaning you can modify them. Tuples, on the other hand, are immutable. This distinction is crucial when dealing with data that should not be altered, like configurations or credentials.

Memory Management in Python:

For network engineers, efficient memory management is crucial, especially when running scripts on resource-constrained network devices. Python offers some key features in this regard:

Automatic Garbage Collection:

GPython automatically clears memory that's no longer in use. For instance, if you've fetched device configurations and stored them in a variable, once done, Python ensures that the memory used by this variable is freed up if it's no longer needed.

Reference Counting:

Every data piece in Python has a reference count. When no references point to data, the memory is released. This is beneficial when looping through large sets of network data; old data gets cleared as new data is processed.

Memory Pools:

Python allocates memory in blocks, not small chunks. This is efficient when handling multiple network packets or logs, ensuring that the script doesn't slow down due to memory fragmentation.

Example for Network Engineers:

Imagine running a Python script that logs into 100 network devices, fetches logs, and processes them. With Python's memory management:

1. As logs from each device are processed, memory used by previous logs is automatically freed.

2. Memory is allocated efficiently, ensuring the script doesn't lag even when processing large logs.

3. There's minimal risk of the script causing memory-related issues on the device.

In essence, Python's memory management ensures your network automation tasks run smoothly without straining device resources.

Conditional Operations: The Ternary Operator:

In Python, the ternary operator allows you to quickly evaluate conditions:

status = "up" if ping_device("") else "down"

This line checks the device's status and assigns "up" or "down" based on the `ping_device` function's result.

Practical Examples for Network Engineers:

Working with Lists:

Suppose you have a list of IP addresses, and you want to ping each one:

ip_addresses = ["", "", ""]
for ip in ip_addresses:
    response = ping_device(ip)
    print(f"IP Address {ip} is {response}")

Using Dictionaries:

Dictionaries are key-value pairs, perfect for storing device configurations:

device_config = {
    "hostname": "Router1",
    "ip_address": "",
    "subnet_mask": ""
print(device_config["hostname"])  # Outputs: Router1
Following is a simple Python program that uses the `paramiko` library to SSH into a router/switch. The program provides two methods for supplying the username and password:

1. Hardcoding the credentials in the script (not recommended for production use due to security concerns).

2. Prompting the user to enter the credentials interactively.

import paramiko

def ssh_to_device(hostname, port, username, password):
    # Create an SSH client instance
    ssh_client = paramiko.SSHClient()
    # Automatically add the server's host key (this is insecure and used for demonstration; in a real-world scenario, verify the host key)
    # Connect to the device
    ssh_client.connect(hostname=hostname, port=port, username=username, password=password)
    # Execute a command (e.g., 'show version') and fetch the output
    stdin, stdout, stderr = ssh_client.exec_command('show version')
    output =
    # Close the SSH connection
    return output

# Method 1: Hardcode the credentials (not recommended for production)
PORT = 22
USERNAME = 'admin'
PASSWORD = 'password123'

# Method 2: Prompt the user for credentials
# USERNAME = input("Enter the username: ")
# PASSWORD = input("Enter the password: ")

# Connect to the device and print the output of 'show version'
print(ssh_to_device(HOSTNAME, PORT, USERNAME, PASSWORD))

**Note**: Before running the script, you'll need to install the `paramiko` library. You can do this using pip:

pip install paramiko



For Telnet, you can use the `telnetlib` library that comes with Python's standard library. The approach would be similar, but the connection and command execution methods would differ.

Remember, hardcoding credentials in scripts is a security risk. Always use secure methods like environment variables, encrypted files, or secret management tools to handle credentials.

In conclusion, Python offers a versatile toolkit for network engineers, simplifying many tasks that were once tedious and error-prone. By understanding these basics, you're well on your way to harnessing Python's full power in your networking endeavors. Stay tuned for more insights, and as always, happy networking and coding! 🌐🐍