基础一:
- #!/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)
跳板机:文章来源:https://www.toymoban.com/news/detail-855346.html
- #!/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中,那么我们可以将该功能封装到一个类中文章来源地址https://www.toymoban.com/news/detail-855346.html
- #!/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】python 模块学习之--Fabric的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!