Fabric SSH Automation

3.2.3 · active · verified Thu Apr 09

Fabric is a high-level Python library for streamlining SSH command execution and application deployment. It is currently at version 3.2.3 and releases updates on an as-needed basis, typically for bug fixes or minor enhancements, building on Invoke and Paramiko.

Warnings

Install

Imports

Quickstart

Create a `fabfile.py` with tasks. Each task receives a `Connection` object `c` as its first argument. Run tasks from the command line using `fab -H user@host task_name`.

# fabfile.py
from fabric import Connection, task

@task
def hello(c):
    """
    Runs a simple command on the remote host.
    Example usage: fab -H user@localhost hello
    
    Note: For 'localhost' to work, an SSH server must be running 
    and your user configured for SSH access.
    """
    print(f"Connecting to {c.host} as {c.user or 'default'}...")
    # Run a command, hiding the command itself from stdout, only showing output
    result = c.run("echo Hello from $(hostname)", hide=True)
    print(f"Output: {result.stdout.strip()}")

view raw JSON →