zoukankan      html  css  js  c++  java
  • fabric批量操作远程操作主机的练习

    fabric是python的一个基于命令行的自动化部署框架,用docker开了两个容器来学习fabric.

    #!/usr/bin/env python
    #-*- coding=utf-8 -*-
    from fabric.api import *
    env.hosts=[
      'root@114.215.86.228:22',
      'root@114.215.86.228:32',
    ]
    env.passwords={
      'root@114.215.86.228:22':'*****',
      'root@114.215.86.228:32':'***',
    }
    @task
    def task1():
      run('ps')
    @task
    def task2():
      run('uname -a')
    @task
    def dotask():
      execute(task1)
      execute(task2)
    

    执行结果

    root@0fe14b5400a8:/home# fab dotask
    [root@114.215.86.228:22] Executing task 'dotask'
    [root@114.215.86.228:22] Executing task 'task1'
    [root@114.215.86.228:22] run: ps
    [root@114.215.86.228:22] out:   PID TTY          TIME CMD
    [root@114.215.86.228:22] out: 26424 pts/2    00:00:00 ps
    [root@114.215.86.228:22] out:
    
    [root@114.215.86.228:32] Executing task 'task1'
    [root@114.215.86.228:32] run: ps
    [root@114.215.86.228:32] out:   PID TTY          TIME CMD
    [root@114.215.86.228:32] out:    59 pts/0    00:00:00 ps
    [root@114.215.86.228:32] out:
    
    [root@114.215.86.228:22] Executing task 'task2'
    [root@114.215.86.228:22] run: uname -a
    [root@114.215.86.228: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
    [root@114.215.86.228:22] out:
    
    [root@114.215.86.228:32] Executing task 'task2'
    [root@114.215.86.228:32] run: uname -a
    [root@114.215.86.228: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
    [root@114.215.86.228:32] out:
    
    [root@114.215.86.228:32] Executing task 'dotask'
    [root@114.215.86.228:22] Executing task 'task1'
    [root@114.215.86.228:22] run: ps
    [root@114.215.86.228:22] out:   PID TTY          TIME CMD
    [root@114.215.86.228:22] out: 26447 pts/2    00:00:00 ps
    [root@114.215.86.228:22] out:
    
    [root@114.215.86.228:32] Executing task 'task1'
    [root@114.215.86.228:32] run: ps
    [root@114.215.86.228:32] out:   PID TTY          TIME CMD
    [root@114.215.86.228:32] out:    63 pts/0    00:00:00 ps
    [root@114.215.86.228:32] out:
    
    [root@114.215.86.228:22] Executing task 'task2'
    [root@114.215.86.228:22] run: uname -a
    [root@114.215.86.228: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
    [root@114.215.86.228:22] out:
    
    [root@114.215.86.228:32] Executing task 'task2'
    [root@114.215.86.228:32] run: uname -a
    [root@114.215.86.228: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
    [root@114.215.86.228:32] out:
    
    
    Done.
    Disconnecting from 114.215.86.228... done.
    Disconnecting from 114.215.86.228:32... done.

    尽管如此,密码是明文,不安全,因此仍然推荐用加key的方式管理服务器

  • 相关阅读:
    Superset 制作图表
    superset 安装配置
    python 虚拟环境 pyenv
    pymysql 单独获取表的栏位名称
    pymysql 返回数据为字典形式(key:value--列:值)
    Oracle/MySQL decimal/int/number 转字符串
    netstat 问题处理
    MySQL 中Index Condition Pushdown (ICP 索引条件下推)和Multi-Range Read(MRR 索引多范围查找)查询优化
    MySQL执行计划extra中的using index 和 using where using index 的区别
    ref与out
  • 原文地址:https://www.cnblogs.com/out8/p/4353056.html
Copyright © 2011-2022 走看看