In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
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!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.