简介
Fabric 是一个 Python 的库,同时它也是一个命令行工具。
它提供了丰富的同 SSH 交互的接口,可以用来在本地或远程机器上自动化、流水化地执行 Shell 命令。
使用 fabric 提供的命令行工具,可以很方便地执行应用部署和系统管理等操作。因此它非常适合用来做应用的远程部署及系统维护。
本章介绍的是fabric3,但是fabric3是fabric1 fork出来支持python3的版本,现在官方已经停止更新,官方现在是fabric2,博客也有专门介绍fabric2的文章。
学习资源:
https://blog.csdn.net/freeking101/article/details/81103945
https://www.cnblogs.com/xiao-apple36/p/9124292.html
https://fabric-chs.readthedocs.io/zh_CN/chs/api/core/docs.html
安装与使用
1.安装
pip uninstall fabric
pip install fabric2
2.一个demo
目录下新建一个文件,命名为fabfile.py
from fabric.api import *
env.hosts = ['120.133.17.123']
env.port = 22
env.user = 'root'
env.password = '123'
def hostname():
run('hostname')
def hi(name,value):
print("hello {0} {1}".format(name,value))
def tail(path='/etc/pas', line=10):
run('tail -n {0} {1}'.format(line, path))
def hello():
with settings(h