最近了解了下fabric ,python 内相似功能 的工具很多,之所以选择 fabric ,第一个主要是它语法不多,上手容易,第二个是 它的语法逻辑,脚本编写更加符合python 的语法习惯,不至于容易跑偏


Fabric is a Python (2.5-2.7) library and command-line tool for streamlining the use of SSH for application deployment or systems administration tasks.

这东西在批量化处理一些远程命令、任务,比较方便,稍微会点python(他是一个python库么,而且一般任务都写成pyton函数的形式)就可以上手,比较简单。


安装起来很简单:

pip install fabric


官网:http://www.fabfile.org



写了个测试例子,代码如下:



#!/usr/bin/env python

# -*- coding: utf-8 -*-

"""

Created on Thu Jan 12 20:09:50 2017

@author: fuyuan

"""

#!/usr/bin/env python

# -*- coding: utf-8 -* 

#fabfile.py

from fabric.api import *

 

env.user = 'tomcat'

env.hosts = ['192.168.200.32']

env.password = 'to****2015'

 

def download():

    local('rm -rf /tmp/newer' )

    local('mkdir /tmp/newer')

     

def pack():

    with lcd('/tmp/'):

         local('tar czvf newer.tar.gz ./newer')

 

def display():

    run('rm -rf /home/tomcat/tmp')

    run('mkdir /home/tomcat/tmp')

    put('/tmp/newer.tar.gz','/home/tomcat/tmp')

 

    with cd('/home/tomcat/tmp'):

         run('tar xvf newer.tar.gz')

run('ls -l ') 

 

def go():

    download()

    pack()

    display()

    

#fab -f spider-fabric1.py  go 



因为基于SSH ,跑起来效果当然不错,简单;但是还发现一个问题,直接本地安装,却无法导入到Pycharm 中,以至于用了其他编辑器来测试首例,后面继续琢磨 如何直接Pycharm 引用本地安装包,毕竟Pycharm 用习惯了;