python fabric多个命令 [英] python fabric multiple commands

查看:202
本文介绍了python fabric多个命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图了解结构如何在一台机器上使用多个命令. 我需要在每个主机(数千个主机)上运行几个命令,并想知道什么是最好的.

I'm trying to understand how fabric is working with multiple commands on one machine. I need to run several commands on each host (thousands of hosts) and would like to know what would be best.

使用多次运行:

res_1 = run(command_1)
res_2 = run(command_2)
...
...
res_n = run(command_n)

或者:

res = run(command_1 && command_2 && ... command_n)
res.splitlines()
res_1 = res[0]
res_2 = res[1]
...
...
res_n = res[n-1]

我想知道的是Fabric如何处理多个运行,它将打开多个会话还是在同一会话中执行所有命令?

What I want to know is how fabric handles multiple runs, will it open multiple sessions or do all commands in the same session?

推荐答案

无论您是使用多个run调用还是对&&进行单个run调用,AFAIK,结构都只会打开一个网络连接.两者之间的区别在于每个新的run在不同的环境中执行.例如,您可以尝试一下.

Regardless of whether you use multiple run calls or a single run call with &&, AFAIK, fabric will open only one network connection. The difference between the two is that each new run executes in a different environment. For example you can try this.

run('ls')
run('cd /tmp/')
run('ls')

这两次都会向您显示您的主目录列表.但是,如果您尝试这样做

Both times it will show you a listing of your home directory. But if you try this

run('ls')
run('cd /tmp/ && ls')

它将第一次显示您的主目录,然后显示/tmp/列表.因此,如果希望将状态从一个命令保留到另一个命令,则应该执行run('cmd1 && cmd1'),但是如果您对此不打扰,则应使用多个run调用.

It will show you your home directory the first time, followed by a listing of /tmp/. Thus if you want the state to be preserved from one command to another you should do run('cmd1 && cmd1') but if you are not bothered about it, you should use multiple run calls.

这篇关于python fabric多个命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆