Project background:
Lab Environment:
VMware Workstation 11
Under the centos6.5 system
Fabric Server Host: ip:192.168.0.14
Fortress Machine: ip:192.168.0.44
Target server: ip:192.168.0.26
SECURECRT (SSH remote connection software)
Software Introduction:
What the fabric website says about it:
Fabric is a Python (2.5-2.7) library and command-line tool for streamlining the use of SSH for application deployment or S Ystems administration tasks.
It provides a basic suite of operations for executing local or remote shell commands (normally or via sudo
) and uploading /downloading files, as well as auxiliary functionality such as prompting the running user for input, or aborting execution .
President
Fabric is an SSH command-line tool based on Python (2.5 to 2.7) that simplifies application deployment and system administration for SSH, and provides a system-based operational component that enables local or remote shell commands, including command execution, file upload, download, and full execution log output.
Experimental process:
First, the software download
[Email protected] ~]# Easy_install fabric #首先你得确保你安装了python-setuptools
Second, check whether the installation is successful
650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M00/7E/AB/wKioL1cHFAyhxYziAAA7BSd-TUk124.png "title=" 01.png "alt=" Wkiol1chfayhxyziaaa7bsd-tuk124.png "/>
You can see that there is no error stating that the module was successfully imported.
Third, the official website Introduction Test Example
1. Sample Map of Fabric website
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/7E/AB/wKioL1cHFJnxqrxSAACTSkv2u9g919.png "title=" 02.png "alt=" Wkiol1chfjnxqrxsaactskv2u9g919.png "/>2, test on your own host
650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/7E/AF/wKiom1cHFDvSP2bhAACrY4xsAMM444.png "title=" 03.png "alt=" Wkiom1chfdvsp2bhaacry4xsamm444.png "/>
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/7E/AB/wKioL1cHFXeShZjjAABrIdw8QTU292.png "title=" 04.png "alt=" Wkiol1chfxeshzjjaabridw8qtu292.png "/> Because I have added SSH trust, I am not prompted to enter a password for the remote host.
If you don't know how to set it, you can look at my article below.
http://9399369.blog.51cto.com/9389369/1750915
Iv. execute commands remotely, and get output on this machine
650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M01/7E/AF/wKiom1cHGDKThTAaAAE7iUoZ71Y633.png "title=" 05.png "alt=" Wkiom1chgdkthtaaaae7iuoz71y633.png "/>" To view local and remote host information.
1. Create a script file
650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/7E/AC/wKioL1cHG1zRk3hgAADWuTDFEUo664.png "title=" 06.png "alt=" Wkiol1chg1zrk3hgaadwutdfeuo664.png "/>
2. Invoking the Local_task task function through the FAB command
650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M02/7E/AC/wKioL1cHG8nBdKSgAADWJ0JjhXo712.png "title=" 07.png "alt=" Wkiol1chg8nbdksgaadwj0jjhxo712.png "/>3, remote_task task function called through FAB command
650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/7E/AF/wKiom1cHG2_hnT5tAACds2lQdDw309.png "title=" 08.png "alt=" Wkiom1chg2_hnt5taacds2lqddw309.png "/> Six, dynamic get remote directory list
1. script File Writing
[Email protected] ~]# vim fuchao02.py
#!/usr/bin/env python
From FABRIC.API Import *
Env.user= "Root"
env.hosts=[' 192.168.0.44 ']
Env.passwd= "123456"
@runs_once
Def input_raw ():
return prompt ("Please input directory name:", default= "/root")
def worktask (dirname):
Run ("Ls-l" +dirname)
@task
def go ():
Getdirname = Input_raw ()
Worktask (Getdirname)
2. Script execution
650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M02/7E/B0/wKiom1cHHZfQyCcbAACT-e_Jq6U385.png "title=" 09.png "alt=" Wkiom1chhzfqyccbaact-e_jq6u385.png "/>
Seven, gateway mode file upload and execution
1, the script file writing
[[email protected] ~]# cat fuchao03.py#!/usr/bin/env pythonfrom fabric.api Import *from fabric.context_managers import *from fabric.contrib.console import confirmenv.user= "root" env.gateway= ' 192.168.0.44 ' #堡垒机ipenv. hosts=[' 192.168.0.26 '] #真正的服务器env. password= "123456" #服务器密码lpackpath = "/home/lnmp1.2.tar.gz" rpackpath = "/home/install" @taskdef put _task (): run ("Mkdir -p /home/install") with settings (warn_only=true): result = put (Lpackpath,rpackpath) if result.failed and not confirm ("Put file failed,continue[y/n]? "): abort (" aborting file put task! ") @taskdef run_task ():         WITH CD ("/home/install"): run ("TAR -ZXVF lnmp1.2.tar.gz ")   WITH CD ("lnmp1.2/"): run ("./install.sh ") @taskdef go (): put_task () run_task ()
2. Script execution
650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M02/7E/AD/wKioL1cHPOXxtPr4AABnZFv4_Ik950.png "title=" 10.png "alt=" Wkiol1chpoxxtpr4aabnzfv4_ik950.png "/>
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/7E/B1/wKiom1cHPHfTRm6SAAF184M_4Kw920.png "title=" 11.png "alt=" Wkiom1chphftrm6saaf184m_4kw920.png "/>650) this.width=650; src=" http://s3.51cto.com/wyfs02/M01/7E/AD/ Wkiol1chpvvdqzuuaaec5g64j-u193.png "title=" 12.png "alt=" Wkiol1chpvvdqzuuaaec5g64j-u193.png "/> can see everything is so simple and convenient ....
Project Summary:
This article from "A few" blog, declined reprint!
Fabric: System batch operation and Maintenance Manager!!!