Connecting to Infrastructure Packages (Netmiko, Boto3, K8s)

🏷️ Final Capstone Engineer Script project / Next Steps After This Curriculum

🌐 Context Introduction

As you move beyond foundational Python skills, the next step is connecting your scripts to the real infrastructure systems you manage daily. This section introduces three essential packages that act as bridges between your Python code and the devices, cloud platforms, and orchestration tools you work with. Each package serves a distinct purpose, but together they form the backbone of modern infrastructure automation.


⚙️ What Are Infrastructure Packages?

Infrastructure packages are pre-built Python libraries that handle the complex communication protocols and API interactions required to manage remote systems. Instead of writing raw socket connections or manual HTTP requests, you can use these packages to send commands, retrieve data, and configure resources with just a few lines of code.

  • Netmiko simplifies SSH connections to network devices like routers, switches, and firewalls
  • Boto3 is the official AWS SDK for managing cloud resources programmatically
  • Kubernetes (k8s) client allows you to interact with Kubernetes clusters for container orchestration

🛠️ Netmiko – Network Device Automation

Netmiko is built on top of Paramiko (an SSH library) and adds device-specific handling for hundreds of network operating systems. It handles the nuances of different prompts, pagination, and command timing so you don't have to.

Key capabilities you will use: - Establishing SSH connections to network devices using device type, hostname, username, and password - Sending show commands and retrieving the output as a string - Sending configuration commands in bulk to apply changes across devices - Handling connection errors and timeouts gracefully with try-except blocks

Typical workflow pattern: - Import the ConnectHandler function from netmiko - Define a device dictionary with connection parameters like device_type, host, username, and password - Create a connection object and use the send_command method for show commands - Use the send_config_set method to push configuration lines from a list


☁️ Boto3 – AWS Cloud Resource Management

Boto3 is the Amazon Web Services (AWS) Software Development Kit (SDK) for Python. It allows you to create, configure, and manage AWS services such as EC2 instances, S3 buckets, Lambda functions, and more directly from your scripts.

Key capabilities you will use: - Listing and describing resources like EC2 instances, S3 buckets, and VPCs - Creating and deleting resources with parameterized configurations - Managing IAM roles, security groups, and access policies - Automating routine tasks like snapshot creation or instance tagging

Typical workflow pattern: - Import boto3 and create a session with your AWS credentials - Use the client or resource interface to interact with specific services - Call methods like describe_instances, create_bucket, or terminate_instances - Handle pagination for large result sets using the Paginator class


🐳 Kubernetes Client – Container Orchestration Control

The official Kubernetes Python client (kubernetes package) gives you programmatic access to your Kubernetes clusters. You can manage pods, deployments, services, namespaces, and other resources without using kubectl commands.

Key capabilities you will use: - Loading kubeconfig files to authenticate with clusters - Listing pods, deployments, and services across namespaces - Creating, updating, and deleting Kubernetes resources - Watching for events and changes in real-time using watch utilities

Typical workflow pattern: - Import config and client from the kubernetes package - Load the kubeconfig using config.load_kube_config for local clusters - Create API client instances like CoreV1Api for pods and services - Use methods like list_namespaced_pod, create_namespaced_deployment, or delete_namespaced_service


📊 Comparison Table – When to Use Each Package

Package Primary Use Case Connection Method Typical Resources Managed
Netmiko Network device configuration and monitoring SSH (port 22) Routers, switches, firewalls, access points
Boto3 AWS cloud resource lifecycle management HTTPS (AWS API endpoints) EC2, S3, Lambda, RDS, IAM
Kubernetes Client Container orchestration and cluster management HTTPS (K8s API server) Pods, deployments, services, namespaces

🕵️ Common Patterns Across All Three Packages

Despite targeting different infrastructure layers, these packages share several design patterns that make them easier to learn together:

  • Client or connection objects – Each package requires you to create a connection or client object that holds authentication and configuration details
  • Method-based operations – Actions are performed by calling methods on the client object (send_command, describe_instances, list_pods)
  • Dictionary-based parameters – Configuration details are passed as Python dictionaries, making them easy to generate dynamically
  • Exception handling – All three packages raise specific exceptions for authentication failures, timeouts, and resource not found errors
  • Pagination support – When dealing with large result sets, each package provides utilities to iterate through pages of results

🔄 How These Packages Fit Into Your Automation Workflow

A typical automation script might combine multiple packages to solve a complete infrastructure problem. For example:

  • Use Boto3 to launch a new EC2 instance in AWS
  • Use Netmiko to SSH into the instance and configure its network settings
  • Use Kubernetes client to register the instance as a node in your cluster

Each package handles its specific domain while your Python script orchestrates the overall workflow, passing data between the different systems as needed.


📚 Next Steps After Learning These Packages

Once you are comfortable with the basics of Netmiko, Boto3, and the Kubernetes client, consider exploring these advanced topics:

  • Error handling and retry logic – Building robust scripts that recover from transient failures
  • Configuration management – Storing device configurations and cloud resource definitions in version control
  • Parallel execution – Using threading or asyncio to manage multiple connections simultaneously
  • Logging and monitoring – Adding structured logging to track what your automation scripts are doing
  • Secret management – Storing credentials securely using environment variables or vault systems

These three packages represent the most common entry points for infrastructure automation, and mastering them will prepare you to tackle almost any automation challenge you encounter.


This section shows how to connect to network devices, cloud services, and container clusters using three common Python packages for engineers.


🔧 Example 1: Simple SSH connection to a network device with Netmiko

This example shows how to connect to a router and run one command.

from netmiko import ConnectHandler

device = {
    "device_type": "cisco_ios",
    "host": "192.168.1.1",
    "username": "admin",
    "password": "password123"
}

connection = ConnectHandler(**device)
output = connection.send_command("show ip interface brief")
connection.disconnect()
print(output)

📤 Output: Interface IP-Address OK? Method Status Protocol
GigabitEthernet0/0 192.168.1.1 YES manual up up
GigabitEthernet0/1 10.0.0.1 YES manual up up


🔧 Example 2: List EC2 instances using Boto3

This example shows how to connect to AWS and list all running EC2 instances.

import boto3

ec2 = boto3.client("ec2", region_name="us-east-1")
response = ec2.describe_instances()

for reservation in response["Reservations"]:
    for instance in reservation["Instances"]:
        print(instance["InstanceId"], instance["State"]["Name"])

📤 Output: i-0a1b2c3d4e5f6g7h8 running
i-1a2b3c4d5e6f7g8h9 stopped


🔧 Example 3: List all pods in a Kubernetes namespace

This example shows how to connect to a Kubernetes cluster and list pods in the default namespace.

from kubernetes import client, config

config.load_kube_config()
v1 = client.CoreV1Api()
pods = v1.list_namespaced_pod(namespace="default")

for pod in pods.items:
    print(pod.metadata.name, pod.status.phase)

📤 Output: nginx-deploy-7f8b9c1d2-abcde Running
redis-pod-5g6h7i8j9-klmno Pending


🔧 Example 4: Send configuration commands to multiple devices with Netmiko

This example shows how to push the same config to two routers at once.

from netmiko import ConnectHandler

devices = [
    {
        "device_type": "cisco_ios",
        "host": "192.168.1.1",
        "username": "admin",
        "password": "password123"
    },
    {
        "device_type": "cisco_ios",
        "host": "192.168.1.2",
        "username": "admin",
        "password": "password123"
    }
]

config_commands = ["interface loopback0", "ip address 10.0.0.1 255.255.255.255"]

for device in devices:
    connection = ConnectHandler(**device)
    output = connection.send_config_set(config_commands)
    print(f"Configured {device['host']}: {output}")
    connection.disconnect()

📤 Output: Configured 192.168.1.1: config term
Enter configuration commands, one per line. End with CNTL/Z.
router(config)# interface loopback0
router(config-if)# ip address 10.0.0.1 255.255.255.255
router(config-if)# end
Configured 192.168.1.2: config term
Enter configuration commands, one per line. End with CNTL/Z.
router(config)# interface loopback0
router(config-if)# ip address 10.0.0.1 255.255.255.255
router(config-if)# end


🔧 Example 5: Create an S3 bucket and upload a file using Boto3

This example shows how to create a storage bucket and upload a local file to AWS.

import boto3

s3 = boto3.client("s3", region_name="us-west-2")
bucket_name = "my-engineer-bucket-2025"

s3.create_bucket(
    Bucket=bucket_name,
    CreateBucketConfiguration={"LocationConstraint": "us-west-2"}
)

s3.upload_file(
    Filename="/home/engineer/config_backup.txt",
    Bucket=bucket_name,
    Key="backups/config_backup.txt"
)

print(f"Bucket {bucket_name} created and file uploaded")

📤 Output: Bucket my-engineer-bucket-2025 created and file uploaded


📊 Comparison Table

Package What It Connects To Typical Use Case
Netmiko Network devices (routers, switches) SSH commands and config push
Boto3 Amazon Web Services (AWS) Cloud resource management
Kubernetes (k8s) Kubernetes clusters Pod, service, and deployment control

🌐 Context Introduction

As you move beyond foundational Python skills, the next step is connecting your scripts to the real infrastructure systems you manage daily. This section introduces three essential packages that act as bridges between your Python code and the devices, cloud platforms, and orchestration tools you work with. Each package serves a distinct purpose, but together they form the backbone of modern infrastructure automation.


⚙️ What Are Infrastructure Packages?

Infrastructure packages are pre-built Python libraries that handle the complex communication protocols and API interactions required to manage remote systems. Instead of writing raw socket connections or manual HTTP requests, you can use these packages to send commands, retrieve data, and configure resources with just a few lines of code.

  • Netmiko simplifies SSH connections to network devices like routers, switches, and firewalls
  • Boto3 is the official AWS SDK for managing cloud resources programmatically
  • Kubernetes (k8s) client allows you to interact with Kubernetes clusters for container orchestration

🛠️ Netmiko – Network Device Automation

Netmiko is built on top of Paramiko (an SSH library) and adds device-specific handling for hundreds of network operating systems. It handles the nuances of different prompts, pagination, and command timing so you don't have to.

Key capabilities you will use: - Establishing SSH connections to network devices using device type, hostname, username, and password - Sending show commands and retrieving the output as a string - Sending configuration commands in bulk to apply changes across devices - Handling connection errors and timeouts gracefully with try-except blocks

Typical workflow pattern: - Import the ConnectHandler function from netmiko - Define a device dictionary with connection parameters like device_type, host, username, and password - Create a connection object and use the send_command method for show commands - Use the send_config_set method to push configuration lines from a list


☁️ Boto3 – AWS Cloud Resource Management

Boto3 is the Amazon Web Services (AWS) Software Development Kit (SDK) for Python. It allows you to create, configure, and manage AWS services such as EC2 instances, S3 buckets, Lambda functions, and more directly from your scripts.

Key capabilities you will use: - Listing and describing resources like EC2 instances, S3 buckets, and VPCs - Creating and deleting resources with parameterized configurations - Managing IAM roles, security groups, and access policies - Automating routine tasks like snapshot creation or instance tagging

Typical workflow pattern: - Import boto3 and create a session with your AWS credentials - Use the client or resource interface to interact with specific services - Call methods like describe_instances, create_bucket, or terminate_instances - Handle pagination for large result sets using the Paginator class


🐳 Kubernetes Client – Container Orchestration Control

The official Kubernetes Python client (kubernetes package) gives you programmatic access to your Kubernetes clusters. You can manage pods, deployments, services, namespaces, and other resources without using kubectl commands.

Key capabilities you will use: - Loading kubeconfig files to authenticate with clusters - Listing pods, deployments, and services across namespaces - Creating, updating, and deleting Kubernetes resources - Watching for events and changes in real-time using watch utilities

Typical workflow pattern: - Import config and client from the kubernetes package - Load the kubeconfig using config.load_kube_config for local clusters - Create API client instances like CoreV1Api for pods and services - Use methods like list_namespaced_pod, create_namespaced_deployment, or delete_namespaced_service


📊 Comparison Table – When to Use Each Package

Package Primary Use Case Connection Method Typical Resources Managed
Netmiko Network device configuration and monitoring SSH (port 22) Routers, switches, firewalls, access points
Boto3 AWS cloud resource lifecycle management HTTPS (AWS API endpoints) EC2, S3, Lambda, RDS, IAM
Kubernetes Client Container orchestration and cluster management HTTPS (K8s API server) Pods, deployments, services, namespaces

🕵️ Common Patterns Across All Three Packages

Despite targeting different infrastructure layers, these packages share several design patterns that make them easier to learn together:

  • Client or connection objects – Each package requires you to create a connection or client object that holds authentication and configuration details
  • Method-based operations – Actions are performed by calling methods on the client object (send_command, describe_instances, list_pods)
  • Dictionary-based parameters – Configuration details are passed as Python dictionaries, making them easy to generate dynamically
  • Exception handling – All three packages raise specific exceptions for authentication failures, timeouts, and resource not found errors
  • Pagination support – When dealing with large result sets, each package provides utilities to iterate through pages of results

🔄 How These Packages Fit Into Your Automation Workflow

A typical automation script might combine multiple packages to solve a complete infrastructure problem. For example:

  • Use Boto3 to launch a new EC2 instance in AWS
  • Use Netmiko to SSH into the instance and configure its network settings
  • Use Kubernetes client to register the instance as a node in your cluster

Each package handles its specific domain while your Python script orchestrates the overall workflow, passing data between the different systems as needed.


📚 Next Steps After Learning These Packages

Once you are comfortable with the basics of Netmiko, Boto3, and the Kubernetes client, consider exploring these advanced topics:

  • Error handling and retry logic – Building robust scripts that recover from transient failures
  • Configuration management – Storing device configurations and cloud resource definitions in version control
  • Parallel execution – Using threading or asyncio to manage multiple connections simultaneously
  • Logging and monitoring – Adding structured logging to track what your automation scripts are doing
  • Secret management – Storing credentials securely using environment variables or vault systems

These three packages represent the most common entry points for infrastructure automation, and mastering them will prepare you to tackle almost any automation challenge you encounter.

Interactive Views

You are currently in 📚 All-in-One mode. Use the tabs at the top to switch to 📖 Theory Only or 💻 Code Only views.

This section shows how to connect to network devices, cloud services, and container clusters using three common Python packages for engineers.


🔧 Example 1: Simple SSH connection to a network device with Netmiko

This example shows how to connect to a router and run one command.

from netmiko import ConnectHandler

device = {
    "device_type": "cisco_ios",
    "host": "192.168.1.1",
    "username": "admin",
    "password": "password123"
}

connection = ConnectHandler(**device)
output = connection.send_command("show ip interface brief")
connection.disconnect()
print(output)

📤 Output: Interface IP-Address OK? Method Status Protocol
GigabitEthernet0/0 192.168.1.1 YES manual up up
GigabitEthernet0/1 10.0.0.1 YES manual up up


🔧 Example 2: List EC2 instances using Boto3

This example shows how to connect to AWS and list all running EC2 instances.

import boto3

ec2 = boto3.client("ec2", region_name="us-east-1")
response = ec2.describe_instances()

for reservation in response["Reservations"]:
    for instance in reservation["Instances"]:
        print(instance["InstanceId"], instance["State"]["Name"])

📤 Output: i-0a1b2c3d4e5f6g7h8 running
i-1a2b3c4d5e6f7g8h9 stopped


🔧 Example 3: List all pods in a Kubernetes namespace

This example shows how to connect to a Kubernetes cluster and list pods in the default namespace.

from kubernetes import client, config

config.load_kube_config()
v1 = client.CoreV1Api()
pods = v1.list_namespaced_pod(namespace="default")

for pod in pods.items:
    print(pod.metadata.name, pod.status.phase)

📤 Output: nginx-deploy-7f8b9c1d2-abcde Running
redis-pod-5g6h7i8j9-klmno Pending


🔧 Example 4: Send configuration commands to multiple devices with Netmiko

This example shows how to push the same config to two routers at once.

from netmiko import ConnectHandler

devices = [
    {
        "device_type": "cisco_ios",
        "host": "192.168.1.1",
        "username": "admin",
        "password": "password123"
    },
    {
        "device_type": "cisco_ios",
        "host": "192.168.1.2",
        "username": "admin",
        "password": "password123"
    }
]

config_commands = ["interface loopback0", "ip address 10.0.0.1 255.255.255.255"]

for device in devices:
    connection = ConnectHandler(**device)
    output = connection.send_config_set(config_commands)
    print(f"Configured {device['host']}: {output}")
    connection.disconnect()

📤 Output: Configured 192.168.1.1: config term
Enter configuration commands, one per line. End with CNTL/Z.
router(config)# interface loopback0
router(config-if)# ip address 10.0.0.1 255.255.255.255
router(config-if)# end
Configured 192.168.1.2: config term
Enter configuration commands, one per line. End with CNTL/Z.
router(config)# interface loopback0
router(config-if)# ip address 10.0.0.1 255.255.255.255
router(config-if)# end


🔧 Example 5: Create an S3 bucket and upload a file using Boto3

This example shows how to create a storage bucket and upload a local file to AWS.

import boto3

s3 = boto3.client("s3", region_name="us-west-2")
bucket_name = "my-engineer-bucket-2025"

s3.create_bucket(
    Bucket=bucket_name,
    CreateBucketConfiguration={"LocationConstraint": "us-west-2"}
)

s3.upload_file(
    Filename="/home/engineer/config_backup.txt",
    Bucket=bucket_name,
    Key="backups/config_backup.txt"
)

print(f"Bucket {bucket_name} created and file uploaded")

📤 Output: Bucket my-engineer-bucket-2025 created and file uploaded


📊 Comparison Table

Package What It Connects To Typical Use Case
Netmiko Network devices (routers, switches) SSH commands and config push
Boto3 Amazon Web Services (AWS) Cloud resource management
Kubernetes (k8s) Kubernetes clusters Pod, service, and deployment control