Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use Fabric Module in python

Shulou Source: shulou.com Published: 2022-06-02 05:22:52 10月23日 Update

This article is about how to use the Fabric module in python. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Basic one:

#! / usr/bin/env python

From fabric.api import *

Env.user='root'

Env.hosts= ['218.78.186.162]

Env.passwords= {'root@218.78.186.162:22':'XXX','root@125.208.12.56:22':'XXXX@0'}

@ runs_once # runs_once representative only executes once

Def local_task ():

Local ("hostname") # local local tasks will not be executed remotely by ssh

Def remote_task ():

With cd ("/ tmp/"):

Run ("hostname") # run remote command

@ task # task tag only go function can call remote_task function

Def go ():

Remote_task ()

test

[root@hostnfsd: / soft/python/pyauto/ Chapter 7 / fabric] $fab-f simple1_test.py remote_task # failed to call the remote_task function directly

Warning: Command (s) not found:

Remote_task

Available commands:

Go

[root@hostnfsd: / soft/python/pyauto/ Chapter 7 / fabric] $fab-f simple1_test.py local_task # if you fail to call the local function directly when it is identified by the task table, you can only call the local function directly when you meitask

Warning: Command (s) not found:

Local_task

Available commands:

Go

[root@hostnfsd: / soft/python/pyauto/ Chapter 7 / fabric] $fab-f simple1_test.py go calls the remote_task function through the go function

[218.78.186.162] Executing task 'go'

[218.78.186.162] run: hostname

[218.78.186.162] out: localhost.localdomain

[218.78.186.162] out:

[125.208.12.56] Executing task 'go'

[125.208.12.56] run: hostname

[125.208.12.56] out: host-192-168156

[125.208.12.56] out:

Done.

Disconnecting from 218.78.186.162... Done.

Disconnecting from 125.208.12.56... Done.

Sometimes we want to execute it directly with a script, which can be changed as follows

#! / usr/bin/env python

From fabric.api import *

Env.user='root'

Env.hosts= ['218.78.186.162]

Env.passwords= {'root@218.78.186.162:22':'ESBecs00','root@125.208.12.56:22':'eRaMUnA612@0'}

@ runs_once

Def local_task ():

Local ("hostname")

Def remote_task ():

With cd ("/ tmp/"):

Run ("hostname")

Def go ():

Execute (remote_task) # execute means it can be executed within the script

Execute (local_task)

Go ()

Just run it directly.

[root@hostnfsd: / soft/python/pyauto/ Chapter 7 / fabric] $python simple1_test.py

Foundation 2:

#! / usr/bin/env python

From fabric.api import *

Env.user='root'

Env.hosts= ['218.78.186.162]

Env.passwords= {'root@218.78.186.162:22':'XXX','root@125.208.12.56:22':'XXXX@0'}

@ runs_once

Def input_raw ():

Return prompt ("please input directory name:", default= "/ home")

Def worktask (dirname):

Run ("ls-l" + dirname)

@ task

Def go ():

Getdirname = input_raw ()

Worktask (getdirname)

Jumping machine:

#! / usr/bin/env python

From fabric.api import *

From fabric.context_managers import *

From fabric.contrib.console import confirm

Env.user='root'

Env.gateway='218.78.186.162'

Env.hosts= ['125.208.12.56']

Env.passwords= {'root@218.78.186.162:22':'XX','root@125.208.12.56:22':'XXXX@0'}

Lpackpath= "/ home/install/lnmp0.9.tar.gz"

Rpackpath= "/ tmp/install"

@ task

Def put_task ():

Run ("mkdir-p / tmp/install")

With settings (warn_only=True):

Result = put (lpackpath, rpackpath)

If result.failed and not confirm ("put file failed, Continue [Ygamma N]?"):

Abort ("Aborting file put task!")

@ task

Def run_task ():

With cd ("/ tmp/install"):

Run ("tar-zxvf lnmp0.9.tar.gz")

Run ("ls-l")

@ task

Def go ():

Put_task ()

Run_task ()

Sometimes we need to write these function templates into django, so we can package the function into a class

#! / usr/bin/env python

From fabric.api import *

Class Student (object):

Def _ init__ (self,user,ip):

Env.user=user

Env.hosts= [ip]

Env.password='XXX'

@ runs_once

Def local_task (self):

Local ("hostname")

Def remote_task (self):

Vhost=run ("df-h")

Return vhost

Def yunxing (user,ip):

Tom=Student (user,ip)

Print execute (tom.remote_task)

Yunxing ('root','218.78.186.162') # call this function directly and pass parameters

Thank you for reading! This is the end of the article on "how to use the Fabric module in python". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

Tags: Functions chapter 7 modules content functions basics more articles scripts good practical representatives tasks only commands articles tags tags templates see Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia MySQL OPPO Reno Docker Huawei