Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to use Fabric to realize automatic operation and maintenance

Shulou Source: shulou.com Published: 2022-06-01 07:11:12 10月22日 Update

This article mainly introduces how to use Fabric to achieve automated operation and maintenance, the article is very detailed, has a certain reference value, interested friends must read it!

Start using Fabric for several simple, repetitive operations, such as logging in to the server, executing commands, and uploading files.

From fabric import Connection

Import traceback

Class FabricUtils:

Def _ _ init__ (self, host: "server IP", userName: "user name", password: "password"):

Self.host = host

Self.userName = userName

Self.password = password

Print (self.userName + "@" + self.host, {"password": self.password})

# initialize ssh link object

Def initClient (self):

Self.con = Connection (self.userName + "@" + self.host, connect_kwargs= {"password": self.password})

# execute shell command

Def runCommand (self, sshCommand: "Linux command line statement"):

# top command has not been tested and passed

# if the command line contains a path, it is best to use an absolute path

Try:

Result = self.con.run (sshCommand, hide=True)

If result.return_code = = 0Rank # error code, 0: correct execution, 1: error

Return True, result.stdout

Return result.failed, result.stdout

Except:

Exp = traceback.format_exc ()

If "Command: 'mkdir" in exp and' File exists' in exp:

Print ("directory [", sshCommand, "] already exists")

Else:

Print (exp)

Return False, exp

# change to a directory

Def cd (self, dir):

Return self.con.cd (dir)

# upload files

Def put (self, src: "the full path of the file to be uploaded. It is best not to have special characters such as spaces in the path", org: "directory saved to the server"):

Return self.con.put (src, org)

If _ _ name__ = ='_ _ main__':

Fabricu = FabricUtils ("server IP", "server login user name", "server login password")

Fabricu.initClient ()

Print (fabricu.runCommand ("tar-zxvf / home/Crawler/WeChatSouGouMain.tar.gz-C / home/Crawler/")

The above code still has context-free problems, such as the cd method, which changes to a directory after execution, but if you execute the put method to upload the file.

If the org parameter uses a relative path, it is not referenced to the switched directory. Such as:

Fabricu.cd ("/ home/crawler")

Fabricu.runCommand ("tar-zxvf / home/Crawler/WeChatSouGouMain.tar.gz-C. /")

After executing the above two pieces of code, the file WeChatSouGouMain.tar.gz is not uploaded to the / home/crawler directory, but is in the root directory of the current user.

The above is all the contents of the article "how to use Fabric to automate operation and maintenance". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Tags: Server directory service command file path user switch login automation code content password method best user name article special up and down context Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB Xiaomi OPPO Reno MySQL Huawei