[远程服务器无响应]多台服务器远程交互的Python库 Fabric 介绍

更新时间:2019-10-05    来源:python    手机版     字体:

【www.bbyears.com--python】

这两天,偶尔再次看了遍这篇文章,并且对Fabric感兴趣,或许我管理一些服务器时也许还能用得上,所以花了点时间简单了解了一下Fabric。

首先,Fabric是什么呢? Fabric是一个Python库和命令行工具,它可以将使用SSH操作的应用部署或系统管理任务更加地高效(流水线化)。
Fabric is a Python (2.5 or higher) library and command-line tool for streamlining the use of SSH for application deployment or systems administration tasks.
Fabric提供了一些核心API和外围API,见:http://fabric.readthedocs.org/en/1.6/index.html#api-documentation
(Fabric还提供了多进程让任务并行执行,是通过Python自带的multiprocessing module来实现的,不过目前并非线程安全的哦。)

然后,安装Fabric试用一下吧。参见:http://fabric.readthedocs.org/en/1.6/installation.html
我是下载源代码,运行“python setup.py install”来安装的,在Ubuntu上也可以使用“sudo apt-get install fabric”来直接安装。

接下来,试一个Helloword这样的例子吧。
编辑一个名为test-fabric.py的文件,内容如下:

 代码如下 from fabric.api import run
 
def host_type():
    run("uname -s")



通过Fabric来执行它,命令行如下:

 代码如下 [root@jay-linux jay]# fab -H jay-linux host_type
 
Fatal error: Couldn"t find any fabfiles!
 
Remember that -f can be used to specify fabfile path, and use -h for help.
 
Aborting.
[root@jay-linux jay]# fab -H jay-linux -f test-fabric.py host_type
[jay-linux] Executing task "host_type"
[jay-linux] run: uname -s
[jay-linux] Login password for "root":
[jay-linux] out: Linux
[jay-linux] out:
 
 
Done.
Disconnecting from jay-linux... done.



执行第一个命令运行时,遇到了“Fatal error: Couldn’t find any fabfiles!”错误,是因为fab没有找到fabfile。
如果没有用“-f test-fabric.py”参数来指定使用哪个fabfile,那么fab命令会默认使用fabfile.py这个名称在当前目前及其父附录中去寻找fabfile.py文件(在Linux上它不搜寻最高层的根目录)。
所以,解决fabfile找到的问题的方法主要有如下3个:
1. “-f your-fabfile.py”来指定自己的fabfile;
2. 就在当前目录中将自己的任务编辑好并命名为 fabfile.py;
3. 可以将默认的fabfile定义在fabric的配置文件,如:在~/.fabricrc文件中指定“fabfile = fab_tasks.py”.
关于Fabric如何查找fabfile的方法,请参考:http://docs.fabfile.org/en/1.6/usage/fabfiles.html#fabfile-discovery
或者可以直接查看源代码中fabric/main.py文件中的find_fabfile(names=None)。

最后,在fabric中可以将主机名、用户名、密码等信息写在fabfile中,但是真的不推荐将明文写在代码中,还是最好使用SSH的Key来认证吧。所以如下我就用Fabric写了一个例子(就在名为fabfile.py文件中),将我的总控机器(my-master)的Key分发到需要被控制的机器上取得SSH key的认证,今后操作那些机器时就不用输入登录密码来交互了。注意“@roles(‘master’)”这样的修饰,这也是Fabric提供的功能,让任务在指定的机器上执行。

 代码如下 #!/usr/bin/python
 
from fabric.colors import red, green
from fabric.context_managers import cd
from fabric.operations import *
from fabric.api import *
 
env.roledefs = {
    "master":["my-master"],
    "slave":["vt9", "vt7", "vt2"]
    }
#env.hosts = ["my-master", "vt9", "vt7", "vt1", "vt2"]
#env.passwords = {"jay-linux":"123456", "my-master":"123456"}
env.password = "123456"
 
def color():
    local("ls -l | wc -l")
    print(red("This sentence is red, except for ", bold=True)
            + green("these words, which are green."))
 
def ctx_mgr():
    with cd("/var/www"):
        run("ls")
 
@roles("master")
def get_sshkey():
    get("/root/.ssh/id_rsa.pub", "id_rsa.pub.master")
 
@roles("slave")
def put_sshkey():
    with cd("/tmp"):
        put("id_rsa.pub.master", "id_rsa.pub.master")
        run("cat id_rsa.pub.master >> /root/.ssh/authorized_keys")
 
def do_task():
    execute(get_sshkey)
    execute(put_sshkey)



该示例程序的,执行如下:

 代码如下 [root@jay-linux jay]# fab do_task
[my-master] Executing task "get_sshkey"
[my-master] download: /root/jay/id_rsa.pub.master <- /root/.ssh/id_rsa.pub
[vt9] Executing task "put_sshkey"
[vt9] put: id_rsa.pub.master -> /tmp/id_rsa.pub.master
[vt9] run: cat id_rsa.pub.master >> /root/.ssh/authorized_keys
[vt7] Executing task "put_sshkey"
[vt7] put: id_rsa.pub.master -> /tmp/id_rsa.pub.master
[vt7] run: cat id_rsa.pub.master >> /root/.ssh/authorized_keys
[vt2] Executing task "put_sshkey"
[vt2] put: id_rsa.pub.master -> /tmp/id_rsa.pub.master
[vt2] run: cat id_rsa.pub.master >> /root/.ssh/authorized_keys
 
Done.
Disconnecting from my-master... done.
Disconnecting from vt7... done.
Disconnecting from vt9... done.
Disconnecting from vt2... done.


总之,使用了一下Fabric,感觉用起来还是比较方便的,用Python定义自己的任务。要是管理的服务器数量较大,并且上面执行的操作比较重复时,Fabric应该是个不错的选择。

参考资料:
Fabric官方文档:http://docs.fabfile.org/en/1.6/index.html

本文来源:http://www.bbyears.com/jiaocheng/71409.html

热门标签

更多>>

本类排行