Fabric: Quick Start
"Fabric is a high level Python (2.7, 3.4+) library designed to execute shell commands remotely over SSH" (https://www.fabfile.org/)
install
$ pip install fabric3
define command
By default, fabfile.py is used to run the script. Create fabfile.py like:
from fabric.api import env, local, run
env.hosts = ['192.168.11.8']
env.user = 'tomo'
env.password = '12345678'
def check():
local('hostname')
run('hostname')
You can list the defined commands:
$ fab -l
Available commands:
check
run command
$ fab check
[192.168.11.8] Executing task 'check'
[localhost] local: hostname
tomo.local
[192.168.11.8] run: hostname
[192.168.11.8] out: myhost
[192.168.11.8] out:
Done.
Disconnecting from 192.168.11.8... done.
upload file
Modify fabfile.py to define another task to use 'put' feature:
from fabric.api import env, local, run, put
env.hosts = ['192.168.11.8']
env.user = 'tomo'
env.password = '12345678'
def check():
local('hostname')
run('hostname')
def upload():
put('hello.txt', '.')
$ echo 'hello' > hello.txt
$ fab upload
[192.168.11.8] Executing task 'upload'
[192.168.11.8] put: hello.txt -> ./hello.txt
Done.
Disconnecting from 192.168.11.8... done.
The file is uploaded to /home/tomo/hello.txt on 192.168.11.8
Comments
Post a Comment