Python的批量远程管理和部署工具Fabric用法实例
本文实例讲述了Python的批量远程管理和部署工具Fabric用法。分享给大家供大家参考。具体如下:
Fabric是Python中一个非常强大的批量远程管理和部署工具,常用于在多个远程PC上批量执行SSH任务.
常见的使用方法大概总结如下:
1. 首先,要将批量执行的任务写入到一个fabfile.py中,
from fabric.api import run, local, roles, env, cd
env.hosts=[
'192.168.1.110',
'192.168.1.111',
'192.168.1.112'
]
env.user="username"
env.password="password"
env.port=22
#env.parallel=True
#env.skip_bad_hosts=True
#env.timeout=1
#env.warn_only=True
# local用于在本地PC执行命令.
# run用于在远程PC执行命令.
def ls():
with cd('/home/workspace/project'):
local('touch 1.log')
with cd('/home/workspace/project2'):
local('touch 2.log')
#@parallel, 可以设置是否并行执行
#@serial
def pull():
with cd('/home/workspace/project'):
run('git pull')
def clean():
with cd('/home/workspace/project'):
run('bash clean.sh')
@hosts('192.168.1.113')
def robot(device):
with cd('/home/workspace/project'):
run('bash run.sh %s robot && sleep 1' % device)
以上就是一个简单的fabfile.py, 其中定义的函数均对应一个fab中的可执行命令.
其中有两个小的注意事项:
A.在远程机器的run.sh中如果要执行一些非系统常见的工具,最好指定为绝对路径. 且可以适当地使用nohup的方式.
B.执行其他脚本或者命令后最好加上sleep,以防止Fabric过早地关闭与远程PC连接的session,而导致执行任务失败.
2. 执行过程: fabric执行会默认选取当前目录下的fabfile.py文件,
fab pull
fab robot:hosts="192.168.1.115",device=5560
可以通过hosts参数给fabric传入指定的远程PC, 该hosts参数的优先级比env.hosts的要高.
也可以给fab中的命令传递参数,如device.
此外,还可以通过fab -f otherFabFile.py clean来指定其他的fabric文件.
如果需要并行执行的话,也可以传递参数如fab -P -z 15 pull, 15表示并行执行的PC数量.
以上,只是一些简单的用法,如果需要更高级的用法,可以关注该项目的github主页 https://github.com/fabric/fabric.
希望本文所述对大家的Python程序设计有所帮助。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Bitcoin’s price fluctuations today are affected by many factors such as macroeconomics, policies, and market sentiment. Investors need to pay attention to technical and fundamental analysis to make informed decisions.

Bitcoin’s price ranges from $20,000 to $30,000. 1. Bitcoin’s price has fluctuated dramatically since 2009, reaching nearly $20,000 in 2017 and nearly $60,000 in 2021. 2. Prices are affected by factors such as market demand, supply, and macroeconomic environment. 3. Get real-time prices through exchanges, mobile apps and websites. 4. Bitcoin price is highly volatile, driven by market sentiment and external factors. 5. It has a certain relationship with traditional financial markets and is affected by global stock markets, the strength of the US dollar, etc. 6. The long-term trend is bullish, but risks need to be assessed with caution.

Gate.io has achieved the transformation from spot trading to on-chain ecosystem through MeMebox 2.0. 1) Build a cross-chain infrastructure and support the interoperability of 12 main chains; 2) Create a DeFi application ecosystem and provide one-stop services; 3) Implement incentive mechanisms and reconstruct value allocation.

The top ten virtual currency trading apps are: 1. OKX, 2. Binance, 3. gate.io, 4. Coinbase, 5. Kraken, 6. Huobi, 7. KuCoin, 8. Bitfinex, 9. Bitstamp, 10. Poloniex. Each platform has outstanding performance in trading products, user experience, security, etc., to meet the needs of different investors.

Recommended reliable digital currency trading platforms: 1. OKX, 2. Binance, 3. Coinbase, 4. Kraken, 5. Huobi, 6. KuCoin, 7. Bitfinex, 8. Gemini, 9. Bitstamp, 10. Poloniex, these platforms are known for their security, user experience and diverse functions, suitable for users at different levels of digital currency transactions

When choosing a compliant and secure Bitcoin trading platform, you need to evaluate its regulatory license, KYC/AML policies and security measures, and recommend three major platforms: Binance, OKX and gate.io.

The top ten safe and easy-to-use virtual currency trading platforms include: 1. OKX, 2. Binance, 3. gate.io, 4. Coinbase, 5. Kraken, 6. Huobi, 7. Bitfinex, 8. KuCoin, 9. Bitstamp, 10. Bittrex. These platforms have their own characteristics and provide registration, deposit, withdrawal and transaction operation guides to ensure the safe and convenient user experience.

The rankings of the top ten trading platforms in the cryptocurrency circle in 2025 are: 1. Binance, 2. OKX, 3. gate.io, 4. Huobi Global, 5. Coinbase, 6. Kraken, 7. Bitfinex, 8. KuCoin, 9. Bybit, 10. Bitstamp. These platforms stand out in the market for their security, transaction volume, user experience and innovation.
