当前位置:   article > 正文

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

【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, Continue[Y/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')   ###直接调用该函数传参即可
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/444738
推荐阅读
相关标签
  

闽ICP备14008679号