Fabric is a very powerful batch remote management and deployment tool in Python that is often used to perform SSH tasks in batches on multiple remote PCs.
The common use methods are summarized as follows:
1, first, to write a batch of tasks into a fabfile.py,
#-*-Coding:utf-8-*-from fabric.api import Run, local, roles, env, cdenv.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 is used to execute commands on the native PC. # Run is used to execute commands on the remote PC. Def ls (): With CD ( '/home/workspace/project '): local (' Touch 1.log ') with CD ('/home/workspace/project2 '): local (' Touch 2. Log ') # @parallel, you can set whether to execute # @serialdef pull () in parallel: 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)
The above is a simple fabfile.py, where the function defined corresponds to an executable command in a fab.
There are two small caveats,
A, in the remote machine's run.sh, if you want to perform some non-system common tools, it is best to specify the absolute path. And can be used appropriately in the Nohup way.
B, it is best to add sleep after executing other scripts or commands to prevent fabric from prematurely shutting down the session connected to the remote PC, causing the task to fail.
2, execution process: Fabric execution will select the fabfile.py file under the current directory by default,
Fab clean
Fab pull
Fab robot:hosts= "192.168.1.115", device=5560
The hosts parameter allows you to pass in the specified remote PC to the fabric, which has a higher priority than env.hosts.
You can also pass parameters, such as device, to commands in the fab.
In addition, additional fabric files can be specified by Fab-f otherfabfile.py clean.
Above, just some simple usage, if you need more advanced usage, you can follow the project's GitHub homepage https://github.com/fabric/fabric.
Batch remote management and deployment tools in fabric---python