您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關python中Fabric模塊怎么用的內容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。
#!/usr/bin/env python
from fabric.api import *
env.user='root'
env.hosts=['218.78.186.162','125.208.12.56']
env.passwords={ 'root@218.78.186.162:22':'XXX','root@125.208.12.56:22':'XXXX@0'}
@runs_once ####runs_once代表只執行一次
def local_task():
local("hostname") ####local本地任務,不會ssh遠程執行
def remote_task():
with cd("/tmp/"):
run("hostname") ###run 遠程命令
@task ####task標記只有go函數可以調用remote_task函數
def go():
remote_task()
測試
[root@hostnfsd :/soft/python/pyauto/第七章/fabric]$ fab -f simple1_test.py remote_task ###直接調用remote_task函數失敗
Warning: Command(s) not found:
remote_task
Available commands:
go
[root@hostnfsd :/soft/python/pyauto/第七章/fabric]$ fab -f simple1_test.py local_task ###有task表標識時直接調用local函數失敗,meitask時才能直接調用local函數
Warning: Command(s) not found:
local_task
Available commands:
go
[root@hostnfsd :/soft/python/pyauto/第七章/fabric]$ fab -f simple1_test.py go 通過go函數調用remote_task函數
[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-168-1-56
[125.208.12.56] out:
Done.
Disconnecting from 218.78.186.162... done.
Disconnecting from 125.208.12.56... done.
有時我們希望直接用腳本就可以執行,可以如下更改
#!/usr/bin/env python
from fabric.api import *
env.user='root'
env.hosts=['218.78.186.162','125.208.12.56']
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表示在腳本內執行即可
execute(local_task)
go()
直接運行即可
[root@hostnfsd :/soft/python/pyauto/第七章/fabric]$ python simple1_test.py
基礎2:
#!/usr/bin/env python
from fabric.api import *
env.user='root'
env.hosts=['218.78.186.162','125.208.12.56']
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)
跳板機:
#!/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[Y/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()
有時需要將這些功能模板寫到django中,那么我們可以將該功能封裝到一個類中
#!/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') ###直接調用該函數傳參即可
感謝各位的閱讀!關于“python中Fabric模塊怎么用”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。