Python3 Fabric (super useful automated o&M module) :

Introduction to the

Fabric is a more powerful SSH automation library in Python. Not only can you debug Linux locally on Windows, but it also supports Python code, which is more flexible than Ansbile

One: Install Python3

1. Go to the Python official website to download the Python installation package. This section uses Linux as an example.

Python – 3.8.0. TGZ Openssl – 1.0.2 j.t. ar. Gz

2. Save the installation package to the /usr/local/page directory (page is user-defined) and run the following command

CD /usr/local/page tar -zxvf python-3.8.0.tgz tar -zxvf openssl-1.0.0.j.tar.gz /usr/local/page/ openssl-1.0.2j. /config make make install Python3.8 CD /usr/local/kernel/ python-3.8.0 ###vi # #zlib zlibmodule.c -i $(prefix)/ include-l $(prefix)/ lib-lz # _socket socketmodule.c timemodule.c #SSL=/usr/local/ssl # _ssl_sl.c \ # -DUSE_SSL -I$(SSL)/include -I$(SSL)/include/openssl \ #-L$(SSL)/lib - LSSL - lcrypto. / configure -- prefix = / usr/local/python3 make make install soft connection to # # ln -s/usr/local/python3 / bin/python3 The/usr/bin/python3 sed - i.b ak '$a export PATH = $PATH: / usr/local/python3 / bin'/etc/profile # # let environment variables take effect (note we will strictly according to the following steps, Otherwise Python3 can't install module) source/etc/profile ln -s/usr/local/Python3 / bin/pip3 / usr/bin/pip3 export LD_LIBRARY_PATH = $LD_LIBRARY_PATH: / usr/local/python3 / lib to join environment variables You then need to recompile: /configure --prefix=/usr/local/python3 --enable-shared make make install pip3 install --index https://pypi.mirrors.ustc.edu.cn/simple/ Fabric3Copy the code

3. Check whether Fabric3 is installed

Fabric3 basic use

1. Basic API usage

There are three fabric wrappers used: run: wrapper for executing remote commands sudo: remote command env with sudo permission: dictionary to save user configuration. (Save the related configuration, such as login username env.user, password env.password, port env.port, etc., if the user name is not specified, the default is to use the current user, port 22)

2. Code examples

The fab -f test.py run is executed locally on Windows using Pycharm. If the py file is named test.py, the Linux machine can run fab -f test.py run by using the following command

import os import sys from collections import OrderedDict from fabric.api import * from fabric.colors import * import ##SET_HOST_INFORMATION## def set_host(): env.roledefs = {'k8s-master1': [' 192.168.1.171], 'k8s - master2: [' 192.168.1.172],' k8s - master3: [' 192.168.1.173], 'k8s - node1: [' 192.168.1.174], 'k8s - 2: [' 192.168.1.175]} env. Passwords = {' [email protected]:22: '12345678', '[email protected]:22': '12345678', '[email protected]:22': '12345678', '[email protected]:22': '12345678', '[email protected]:22: @roles('k8s-master1', 'k8s-master2', 'k8S-master3 ', 'k8s-node1', @roles(' k8s-master3', 'k8s-node1') 'k8s-node2') def shut_down(): sudo("init 0") # def run(): Execute (shut_down) if __name__ == "__main__": run()Copy the code