Fabric quickly

环境很重要、环境很重要、环境很重要

# CentOS 7
$ setenforce 0  # 可以设置配置文件永久关闭
$ systemctl stop iptables.service
$ systemctl stop firewalld.service

一. 准备 Python3 和 Python 虚拟环境

1.1 安装依赖包

$ yum -y install wget sqlite-devel xz gcc automake zlib-devel openssl-devel epel-release git

1.2 编译安装

$ wget https://www.python.org/ftp/python/3.6.1/Python-3.6.1.tar.xz
$ tar xvf Python-3.6.1.tar.xz  && cd Python-3.6.1
$ ./configure && make && make install

1.3 建立 Python 虚拟环境

因为 CentOS 6/7 自带的是 Python2,而 Yum 等工具依赖原来的 Python,为了不扰乱原来的环境我们来使用 Python 虚拟环境

$ cd /opt
$ python3 -m venv py3
$ source /opt/py3/bin/activate

# 看到下面的提示符代表成功,以后运行 Jumpserver 都要先运行以上 source 命令,以下所有命令均在该虚拟环境中运行
(py3) [root@localhost py3]

二、Fabric 安装和使用

  fabric版本:(十分重要)
  pip install fabric3 or pip3 install fabric3

  常用命令:

  fab -u root -p '123456.' -H ‘10.0.0.1’ -- 'free -m'

  常用脚本格式:

 1 #!/usr/bin/python env
 2 # -*- coding: utf-8 -*-
 3 from fabric.api import *
 4 
 5 env.hosts = ["192.168.1.108"]
 6 env.user = "root"
 7 env.password = "123456"
 8 env.port = 22
 9 
10 @task
11 def show():
12         run("df -lh")
13         run("free -m")
14         run("hostname")
View Code

猜你喜欢

转载自www.cnblogs.com/ipyanthony/p/9276014.html