Getting Started With the Fabric Python Library

Fabric is a Python library and command-line tool for streamlining the use of SSH for application deployment or systems administration tasks. Fabric is very simple and powerful and can help to automate repetitive command-line tasks. This approach can save time by automating your entire workflow. 

This tutorial will cover how to use Fabric to integrate with SSH and automate tasks.


Fabric is best installed via pip:

Getting Started With Fabric


Below is a simple function demonstrating how to use Fabric.

The program above is then saved as in your current working directory. The welcome function can be executed with the fab tool as follows:

Fabric provides the fab command which reads its configuration from a file, The file should be in the directory from which the command is run. A standard fabfile contains the functions to be executed on a remote host or a group of remote hosts.


Fabric implements functions which can be used to communicate with remote hosts:

This operation is used to run a shell command on a remote host.



This function is used to download file(s) from a remote host. The example below shows how to download a backup from a remote server.


This functions uploads file(s) to a remote host. For example:


As the name suggests, this function reboots a system server.


This function is used to execute commands on a remote host with superuser privileges. Additionally, you can also pass an additional user argument which allows you to run commands as another user other than root.



This function is used to run a command on the local system. An example is:


The function prompts the user with text and returns the input.



This function is used to check for given keys in a shared environment dict. If not found, the operation is aborted.

SSH Integration

One of the ways developers interact with remote servers besides FTP clients is through SSH. SSH is used to connect to remote servers and do everything from basic configuration to running Git or initiating a web server.

With Fabric, you can perform SSH activities from your local computer.

The example below defines functions that show how to check free disk space and host type. It also defines which host will run the command:

In order to run this code, you will need to run the following command on the terminal:


Automating Tasks

Fabric enables you to run commands on a remote server without needing to log in to the remote server.

Remote execution with Fabric can lead to security threats since it requires an open SSH port, especially on Linux machines.

For instance, let’s assume you want to update the system libraries on your remote server. You don’t necessarily need to execute the tasks every other time. You can just write a simple fab file which you will run every time you want to execute the tasks.

In this case, you will first import the Fabric API’s module:

Define the remote host you want to update:

Set the username of the remote host:

Although it’s not recommended, you might need to specify the password to the remote host.

Lastly, define the function that updates the libraries in your remote host.

Now that your fab file is ready, all you need to do is execute it as follows:

You should see the following result:

If you didn’t define the password, you will be prompted for it.

After the program has finished executing the defined commands, you will get the following response, if no errors occur:


This tutorial has covered what is necessary to get started with Fabric locally and on remote hosts. You can now confidently start writing your own scripts for building, monitoring or maintaining remote servers.

Powered by WPeMatico

Leave a Comment

Scroll to Top