DevOps Engineer. Solutions Architect

Blog 4 Spring 2021: Fabric SSH Python Module

Fabric is a high level Python module built on top of Paramiko to execute hell commands remotely over SSH. Those familiar with Paramiko are able to understand the underlying similarities between the two SSH modules. Similar to Paramiko, one can import the library into a Python script or install it directly to their Python interpreter.

Here are some example code modules:

Run singular commands on an individual host instance
 »> result = Connection(‘web1’).run(‘hostname’) web1  »> result

Single commands across multiple hosts
 »> from fabric import SerialGroup  »> result = SerialGroup(‘web1’, ‘web2’).run(‘hostname’) web1 web2  »> # Sorting for consistency…it’s a dict!  »> sorted(result.items()) (, ), ...]

Here are examples covering Fabric imported into a Python script
import Fabric  »> def disk_free(c): … uname = c.run(‘uname -s’, hide=True) … if ‘Linux’ in uname.stdout: … command = “df -h / | tail -n1 | awk ‘{print $5}’” … return c.run(command, hide=True).stdout.strip() … err = “No idea how to get disk space on {}!”.format(uname) … raise Exit(err) …  »> print(disk_free(Connection(‘web1’)))

Running on multiple hosts
 »> # NOTE: Same code as above!  »> def disk_free(c): … uname = c.run(‘uname -s’, hide=True) … if ‘Linux’ in uname.stdout: … command = “df -h / | tail -n1 | awk ‘{print $5}’” … return c.run(command, hide=True).stdout.strip() … err = “No idea how to get disk space on {}!”.format(uname) … raise Exit(err) …  »> for cxn in SerialGroup(‘web1’, ‘web2’, ‘db1’): … print(“{}: {}”.format(cxn, disk_free(cxn))) : 33% : 17% : 2%

First and foremost it’s important to understand the capabilities of this library. It’s capable of automating and provisioning multiple instances directly from your local machine. Multiple tasks may be given in a single CLI session, through the use of fab build deploy. Just setup a fabfile and setup hostname initialization to connect to your servers/instances.