Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 如何在变量中保存Azure cli命令的输出_Python 3.x_Azure_Azure Cli - Fatal编程技术网

Python 3.x 如何在变量中保存Azure cli命令的输出

Python 3.x 如何在变量中保存Azure cli命令的输出,python-3.x,azure,azure-cli,Python 3.x,Azure,Azure Cli,在python 3.5中使用azure cli并从脚本调用命令时,我无法控制控制台中的输出 当执行一个命令时,它会将结果打印到控制台,但我很难将结果放入一个变量中进行分析 from azure.cli.core import get_default_cli class AzureCmd(): def __init__(self, username, password): self.username = username self.

在python 3.5中使用azure cli并从脚本调用命令时,我无法控制控制台中的输出

当执行一个命令时,它会将结果打印到控制台,但我很难将结果放入一个变量中进行分析

from azure.cli.core import get_default_cli

class AzureCmd():
        def __init__(self, username, password):
            self.username = username
            self.password = password

        def login(self, tenant):
            login_successfull = get_default_cli().invoke(['login',
                                                          '--tenant', tenant,
                                                          '--username', self.username,
                                                          '--password', self.password]) == 0
            return login_successfull

        def list_vm(self, tenant):
            list_vm = get_default_cli().invoke(['vm', 'list', '--output', 'json'])
            print(list_vm)


    tenant = 'mytenant.onmicrosoft.com'
    cmd = AzureCmd('login', 'mypassword')
    cmd.login(tenant)
    cmd.list_vm(tenant)
这是我的脚本尝试

我想要实现的是:在执行cmd.login(租户)时不会得到任何输出。
我想将get_default_cli().invoke()的输出保存在其中,而不是在我的变量login_successfull and list_vm中获取0(成功)或1(失败)。

我认为您可以使用子流程并调用az cli来获取输出,而不是使用get_default_cli

import subprocess
import json

process = subprocess.Popen(['az','network', 'ddos-protection', 'list'], stdout=subprocess.PIPE)
out, err = process.communicate()
d = json.loads(out)
print(d)

我遇到了同样的问题,并找到了解决方案,我还发现许多人提供了在大多数情况下正常工作的标准解决方案,但他们没有验证它是否适用于这种情况,结果证明az cli是一种边缘解决方案
我认为问题与az cli基于python有关

Win10CommandPrompt:\>其中
C:\ProgramFiles(x86)\Microsoft SDK\Azure\CLI2\wbin\az.cmd

如果查看该文件,您将看到类似的内容,并发现Azure CLI只是python:

python.exe -IBm azure.cli
所以要做你想做的事,试试这个(它对我很有用):

除非每个参数都是逗号分隔的字符串列表,否则上述语法将无法工作。在阅读了如何使用python popen执行多个参数后,我发现了一种我更喜欢的语法:

import subprocess
azcmd = "az ad sp create-for-rbac --name " + SPName + " --scopes /subscriptions/" + subscriptionid
out = subprocess.run(azcmd, shell=True, stdout=subprocess.PIPE).stdout.decode('utf-8')
print(out)

我们可以在Python中执行Azure CLI命令,如下所示。 在这里,res变量通常存储整数类型的值,因此我们可能无法访问json响应。要将响应存储在变量中,我们需要执行cli.result.result

from azure.cli.core import get_default_cli

def az_cli(args_str):
    args = args_str.split()
    cli = get_default_cli()
    res = cli.invoke(args)```
    if cli.result.result:
       jsondata = cli.result.result
    elif cli.result.error:
       print(cli.result.error)

如果成功,则返回0。它由python sdk中的get_default_cli().invoke()函数实现。如果您尝试使用该功能,似乎无法获得命令输出,您可以将您的命令发送给azure团队。尝试一下!非常感谢:)
from azure.cli.core import get_default_cli

def az_cli(args_str):
    args = args_str.split()
    cli = get_default_cli()
    res = cli.invoke(args)```
    if cli.result.result:
       jsondata = cli.result.result
    elif cli.result.error:
       print(cli.result.error)