Show Menu
Cheatography

Python Fabric Cheat Sheet (DRAFT) by

Fabric is a library built on top of Invoke and Paramiko which provide high-level classes and functions for a Python script to execute command remotely via ssh.

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Instal­lation

pip install fabric

Quick Start

import fabric

with fabric.Connection(host="primary") as connection:
    # Run and suppress (hide) the output
    result = connection.run("whoami", hide=True)
    user = result.stdout.strip()
    print(f"{user=}")

Connection Object Common __init__ Keywords

-
host

-
user

-
port

-
config

-
gateway

-
ssh_config

-
connec­t_t­imeout

-
connec­t_k­wargs

Connection Object Common Attributes

-
host

-
user

-
port

-
is_con­nected

-
ssh_config

Connection Object Common Methods

get(args, *kwargs)    # Download
put(args, *kwargs)    # Upload
run(command, **kargs)   # Run a command, return a result
sftp()                  # Opens a SFTP connection
sudo(command, **kargs)  # Run a command as root