fabric批量操作遠端操作主機的練習

2021-09-06 21:39:47 字數 3126 閱讀 5182

fabric是python的乙個基於命令列的自動化部署框架,用docker開了兩個容器來學習fabric.

#

!/usr/bin/env python

#-*- coding=utf-8 -*-

from fabric.api import *env.hosts=[

'[email protected]:22',

'[email protected]:32',

]env.passwords=

@task

deftask1():

run(

'ps'

)@task

deftask2():

run(

'uname -a')

@task

defdotask():

execute(task1)

execute(task2)

執行結果

root@0fe14b5400a8:/home#

fab dotask

[[email protected]:22] executing task '

dotask

'[[email protected]:22] executing task '

task1

'[[email protected]:22] run: ps

[[email protected]:22] out: pid tty time cmd

[[email protected]:22] out: 26424 pts/2 00:00:00ps

[[email protected]:22] out:

[[email protected]:32] executing task '

task1

'[[email protected]:32] run: ps

[[email protected]:32] out: pid tty time cmd

[[email protected]:32] out: 59 pts/0 00:00:00ps

[[email protected]:32] out:

[[email protected]:22] executing task '

task2

'[[email protected]:22] run: uname -a

[[email protected]:22] out: linux iz2893wjzgyz 3.13.0-32-generic #

57-ubuntu smp tue jul 15 03:51:08 utc 2014 x86_64 x86_64 x86_64 gnu/linux

[[email protected]:22] out:

[[email protected]:32] executing task '

task2

'[[email protected]:32] run: uname -a

[[email protected]:32] out: linux c368a2768e6b 3.13.0-32-generic #

57-ubuntu smp tue jul 15 03:51:08 utc 2014 x86_64 x86_64 x86_64 gnu/linux

[[email protected]:32] out:

[[email protected]:32] executing task '

dotask

'[[email protected]:22] executing task '

task1

'[[email protected]:22] run: ps

[[email protected]:22] out: pid tty time cmd

[[email protected]:22] out: 26447 pts/2 00:00:00ps

[[email protected]:22] out:

[[email protected]:32] executing task '

task1

'[[email protected]:32] run: ps

[[email protected]:32] out: pid tty time cmd

[[email protected]:32] out: 63 pts/0 00:00:00ps

[[email protected]:32] out:

[[email protected]:22] executing task '

task2

'[[email protected]:22] run: uname -a

[[email protected]:22] out: linux iz2893wjzgyz 3.13.0-32-generic #

57-ubuntu smp tue jul 15 03:51:08 utc 2014 x86_64 x86_64 x86_64 gnu/linux

[[email protected]:22] out:

[[email protected]:32] executing task '

task2

'[[email protected]:32] run: uname -a

[[email protected]:32] out: linux c368a2768e6b 3.13.0-32-generic #

57-ubuntu smp tue jul 15 03:51:08 utc 2014 x86_64 x86_64 x86_64 gnu/linux

[[email protected]:32] out:

done.

disconnecting

from 114.215.86.228... done.

disconnecting

from 114.215.86.228:32... done.

儘管如此,密碼是明文,不安全,因此仍然推薦用加key的方式管理伺服器。

Fabric批量遠端執行操作

最近有個需求就是要在乙個集群的多個機器上執行一些命令,比如啟動 停止服務,執行一些指令碼收集一些資料等,於是找到了python的乙個框架fabric。fabric是乙個python庫,用於簡化使用ssh的應用程式部署或系統管理任務。在我的centos上,執行下面的命令就可以簡單安裝 yum inst...

Mysql操作主鍵

1.查詢主鍵 select t.table name,t.constraint type,c.column name,c.ordinal position from information schema.table constraints as t,information schema.key co...

python操作mycat只操作主庫

測試 1 將資料庫配置成主從賦值,mycat實現讀寫分離 2 使用python對資料庫進行操作 3 主從資料庫都開啟general log,檢視資料庫查詢日誌。實驗結果 使用mysqldb和pymysql庫,不管什麼操作,mycat都將操作路由到了主庫來執行。原因 使用python第三方庫,mysq...