您好,登录后才能下订单哦!
这篇文章给大家分享的是有关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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。