I’m using Xubuntu 12.04.03 32bit
What is Fabric?
In this tutorial I’m using fabric to:
make whoami command to every user
method that puts file/folder to user
method that gets file/folder from user
I have done 4 users and created public key authentication to them. You can see how I have done it from this tutorial:
Public key authentication over SSH
Install Fabric from apt
$ sudo apt-get update && sudo apt-get -y install fabric
Or you can get it from pip.
$ sudo apt-get update && sudo apt-get -y install python-pip
$ sudo pip install fabric
Whoami
Create fabric folder and fabfile.py
$ mkdir fabric
$ cd fabric/
$ nano fabfile.py
Create fabfile what runs command whoami to every user over ssh.
from fabric.api import env , run
env.hosts=[ "soivite01@localhost" , "soivite02@localhost" ,
"soivite03@localhost" , "soivite04@localhost" ]
#env.password="password"
env.parallel= True
def whoami_check() :
run( "whoami" )
With fab -l you can see all commands
$ fab -l
Available commands:
whoami_check
Run whoami_check with fabric.
$ fab whoami_check
[ soivite01@localhost] Executing task 'whoami_check'
...
[ soivite04@localhost] run: whoami
...
[ soivite01@localhost] out: soivite01
[ soivite02@localhost] out: soivite02
[ soivite03@localhost] out: soivite03
[ soivite04@localhost] out: soivite04
Done.
Using time before the command you can see how much time is used to run fabric command.
$ time fab whoami_check
...
real 0m2.234s
user 0m0.692s
sys 0m0.060s
Put and get files
Create example file that you can put to users and get that same file from users.
$ nano sendFile.txt
Hello World
Modify fabfile. Add file_put and file_get methods.
$ nano fabfile.py
Add put and get to imports
from fabric.api import env , run, put, get
If you don’t want to give every import by hand you can use this too
from fabric.api import *
Add file_put and file_get methods to end of the class.
...
def file_put() :
put( "sendFile.txt" )
def file_get() :
get( "sendFile.txt" )
Run file_put so your adding files to users.
$ fab file_put
[ soivite01@localhost] Executing task 'file_put'
[ soivite01@localhost] put: sendFile.txt -> /home/soivite01/sendFile.txt
...
Done.
Disconnecting from soivite01@localhost... done .
...
Run file_get to get same files what you have added.
$ fab file_get
[ soivite01@localhost] Executing task 'file_get'
[ soivite01@localhost] download: /home/xubuntu/fabric/soivite01@localhost/sendFile.txt <- /home/soivite01/sendFile.txt
...
Done.
Disconnecting from soivite01@localhost... done .
...
Your fabric folder now looks like this.
fabric/
├── fabfile.py
├── fabfile.pyc
├── sendFile.txt
├── soivite01@localhost
│ └── sendFile.txt
├── soivite02@localhost
│ └── sendFile.txt
├── soivite03@localhost
│ └── sendFile.txt
└── soivite04@localhost
└── sendFile.txt
Put and get folders
Create folder and couple files in there
$ mkdir testFolder && touch testFolder/file1 testFolder/file2
Modify fabfile
$ nano fabfile.py
Create method that puts folder to users and gets it.
...
def folder() :
put( "testFolder" )
get( "testFolder" )
Run command
$ fab folder
[ soivite01@localhost] Executing task 'folder'
[ soivite01@localhost] put: testFolder/file2 -> /home/soivite01/testFolder/file2
[ soivite01@localhost] put: testFolder/file1 -> /home/soivite01/testFolder/file1
[ soivite01@localhost] download: /home/xubuntu/fabric/soivite01@localhost/testFolder/file1 <- /home/soivite01/testFolder/file1
[ soivite01@localhost] download: /home/xubuntu/fabric/soivite01@localhost/testFolder/file2 <- /home/soivite01/testFolder/file2
...
Your folder tree looks like this.
fabric/
├── fabfile.py
├── fabfile.pyc
├── sendFile.txt
├── soivite01@localhost
│ ├── sendFile.txt
│ └── testFolder
│ ├── file1
│ └── file2
├── soivite02@localhost
│ ├── sendFile.txt
│ └── testFolder
│ ├── file1
│ └── file2
├── soivite03@localhost
│ ├── sendFile.txt
│ └── testFolder
│ ├── file1
│ └── file2
├── soivite04@localhost
│ ├── sendFile.txt
│ └── testFolder
│ ├── file1
│ └── file2
└── testFolder
├── file1
└── file2
Your fabfile.py looks like this
from fabric.api import env , run, put, get
env.hosts=[ "soivite01@localhost" , "soivite02@localhost" ,
"soivite03@localhost" , "soivite04@localhost" ]
#env.password="password"
env.parallel= True
def whoami_check() :
run( "whoami" )
def file_put() :
put( "sendFile.txt" )
def file_get() :
get( "sendFile.txt" )
def folder() :
put( "testFolder" )
get( "testFolder" )
Next tutorial you learn how to use sudo:
Using sudo in Fabric
Source:
Fabric tutorial 1 – Take command of your network