Fabric is a python-based SSH command-line tool that simplifies application deployment and system administration tasks for SSH, which provides a system-based operational component that enables local or remote shell commands, including command execution, file upload, download, and full execution log output. The fabric is built on top of the Paramiko, making it easier to operate.
Command description
Command format:
Fab [options] <command>[:arg1,arg2=val2,host=foo,hosts= ' h1;h2 ',...] ...
-L #显示定义好的任务函数名-F #指定fab入口文件, the default entry file name is Fabfile.py-f #指定网关 (transit) device, such as the Bastion machine environment, fill the bastion machine IP can-H #指定目标主机, multiple hosts with ', ' separated-p #远程账号的密码, Fab executes by default using the root account-P #以异步并行方式运行多主机任务, the default is serial run-R #指定role (role), differentiated by role name different business group devices-T #设置设备连接超时时间 (seconds)-T #设置远程 Host command execution time-out (seconds)-W #当命令执行失败, warning instead of default abort task.
Simple example
1. View memory usage size for local and remote hosts
192.168.100.242 is native, 192.168.100.245 is a remote host
[Email protected]:/usr/local/sbin# cat fab1.py#!/usr/bin/env pythonfrom fabric.api Import *env.hosts = [' 192.168.100.242 ', ' 192.168.100.245 ']env.user = ' root ' env.passwords = {' [email protected]:22 ': ' Root ', ' [email protected ]:22 ': ' abc123 '} @runs_oncedef local_mem (): local (' free-m ') def remote_mem (): Run (' free-m ')
To view the commands that can be executed:
[Email protected]:/usr/local/sbin# fab-f fab1.py-lavailable Commands:local_mem remote_mem
Here is the result of the execution:
[email protected]:/usr/local/sbin# fab -f fab1.py local_mem[192.168.100.242] executing task ' Local_mem ' [localhost] local: free -m total used free shared buffers cachedMem: 48294 35086 13207 4 1775 31047-/+ buffers/cache: 2263 46031Swap: 0 &nbSp;0 0done.
[Email protected]:/usr/local/sbin# fab -f fab1.py remote_mem[localhost] local: free -m total used free shared buffers cachedMem: 48294 35087 13206 4 1776 31049-/+ buffers/cache: 2262 46031Swap: 0 0 0done. [192.168.100.245] run: free -m[192.168.100.245] out: total used free shared buffers cached[192.168.100.245] out: Mem: 984 890 93 0 &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;188&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;208[192.168.100.245] out: -/+ buffers/cache: 493 490[192.168.100.245] out: Swap: 3071 0 3071[ 192.168.100.245] out:done.
This article from "Little Fart Babe" blog, declined reprint!
Python's Fabric module