【python】python 模块学习之--Fabric

基础一:

  1. #!/usr/bin/env python
  2. from fabric.api import *
  3. env.user='root'
  4. env.hosts='218.78.186.162','125.208.12.56'
  5. env.passwords={ 'root@218.78.186.162:22':'XXX','root@125.208.12.56:22':'XXXX@0'}
  6. @runs_once ####runs_once代表只执行一次
  7. def local_task():
  8. local("hostname") ####local本地任务,不会ssh远程执行
  9. def remote_task():
  10. with cd("/tmp/"):
  11. run("hostname") ###run 远程命令
  12. @task ####task标记只有go函数可以调用remote_task函数
  13. def go():
  14. remote_task()

测试

  1. root@hostnfsd :/soft/python/pyauto/第七章/fabric$ fab -f simple1_test.py remote_task ###直接调用remote_task函数失败
  2. Warning: Command(s) not found:
  3. remote_task
  4. Available commands:
  5. go
  6. root@hostnfsd :/soft/python/pyauto/第七章/fabric$ fab -f simple1_test.py local_task ###有task表标识时直接调用local函数失败,meitask时才能直接调用local函数
  7. Warning: Command(s) not found:
  8. local_task
  9. Available commands:
  10. go
  11. root@hostnfsd :/soft/python/pyauto/第七章/fabric$ fab -f simple1_test.py go 通过go函数调用remote_task函数
  12. 218.78.186.162 Executing task 'go'
  13. 218.78.186.162 run: hostname
  14. 218.78.186.162 out: localhost.localdomain
  15. 218.78.186.162 out:
  16. 125.208.12.56 Executing task 'go'
  17. 125.208.12.56 run: hostname
  18. 125.208.12.56 out: host-192-168-1-56
  19. 125.208.12.56 out:
  20. Done.
  21. Disconnecting from 218.78.186.162... done.
  22. Disconnecting from 125.208.12.56... done.

有时我们希望直接用脚本就可以执行,可以如下更改

  1. #!/usr/bin/env python
  2. from fabric.api import *
  3. env.user='root'
  4. env.hosts='218.78.186.162','125.208.12.56'
  5. env.passwords={ 'root@218.78.186.162:22':'ESBecs00','root@125.208.12.56:22':'eRaMUnA612@0'}
  6. @runs_once
  7. def local_task():
  8. local("hostname")
  9. def remote_task():
  10. with cd("/tmp/"):
  11. run("hostname")
  12. def go():
    execute(remote_task) ####execute表示在脚本内执行即可
  13. execute (local_task)
    go()

直接运行即可

root@hostnfsd :/soft/python/pyauto/第七章/fabric$ python simple1_test.py

基础2:

  1. #!/usr/bin/env python
  2. from fabric.api import *
  3. env.user='root'
  4. env.hosts='218.78.186.162','125.208.12.56'
  5. env.passwords={ 'root@218.78.186.162:22':'XXX','root@125.208.12.56:22':'XXXX@0'}
  6. @runs_once
  7. def input_raw():
  8. return prompt("please input directory name:",default="/home")
  9. def worktask(dirname):
  10. run("ls -l "+dirname)
  11. @task
  12. def go():
  13. getdirname = input_raw()
  14. worktask(getdirname)

跳板机:

  1. #!/usr/bin/env python
  2. from fabric.api import *
  3. from fabric.context_managers import *
  4. from fabric.contrib.console import confirm
  5. env.user='root'
  6. env.gateway='218.78.186.162'
  7. env.hosts='125.208.12.56'
  8. env.passwords={ 'root@218.78.186.162:22':'XX','root@125.208.12.56:22':'XXXX@0'}
  9. lpackpath="/home/install/lnmp0.9.tar.gz"
  10. rpackpath="/tmp/install"
  11. @task
  12. def put_task():
  13. run("mkdir -p /tmp/install")
  14. with settings(warn_only=True):
  15. result = put(lpackpath, rpackpath)
  16. if result.failed and not confirm("put file failed, ContinueY/N?"):
  17. abort("Aborting file put task!")
  18. @task
  19. def run_task():
  20. with cd("/tmp/install"):
  21. run("tar -zxvf lnmp0.9.tar.gz")
  22. run("ls -l")
  23. @task
  24. def go():
  25. put_task()
  26. run_task()

有时需要将这些功能模板写到django中,那么我们可以将该功能封装到一个类中

  1. #!/usr/bin/env python
  2. from fabric.api import *
  3. class Student(object):
  4. def init(self,user,ip):
  5. env.user=user
  6. env.hosts=ip
  7. env.password='XXX'
  8. @runs_once
  9. def local_task(self):
  10. local("hostname")
  11. def remote_task(self):
  12. vhost=run("df -h")
  13. return vhost
  14. def yunxing(user,ip):
  15. tom=Student(user,ip)
  16. print execute(tom.remote_task)
  17. yunxing('root','218.78.186.162') ###直接调用该函数传参即可
相关推荐
一隅论数智2 天前
给AI一张“业务概念地图“:本体如何从哲学走向企业智能
大数据·人工智能·经验分享·笔记·学习·学习方法·政务
默_笙2 天前
🍙 给每个请求过安检:FastAPI 是怎么把校验写进类型注解的
python
XiHongShi20162 天前
STM32F407 RTC定时器例程,建议保存
stm32·单片机·学习
qq_426003962 天前
启动playwright录制codegen生成自动化测试脚本
python·自动化
虎头金猫2 天前
4K 视频总卡在公网带宽?用 N1 + OpenList 把网盘播放链路重新理顺
运维·服务器·网络·python·容器·beautifulsoup·pandas
长沙三为智能科技2 天前
家政小程序开发从0到上线:五阶段交付流程与验收清单
python
爱吃苹果的日记本2 天前
离散数学第六课
学习·离散数学
伞伞悦读2 天前
【第38期】Python 模块与包详解:import、from、模块搜索路径、包结构和 __init__
开发语言·python
只睡四小时2 天前
Canvas 弹道联机实战:700 行 + 固定时间步长
python·websocket·html5·游戏开发·canvas