gpt4 book ai didi

azure - 使用 python sdk 在 azure 中的 Linux 虚拟机中运行命令

转载 作者:行者123 更新时间:2023-12-01 01:47:19 25 4
gpt4 key购买 nike

我发现azure python sdk提供了以下在linux虚拟机中运行命令的方法。

from azure.mgmt.compute import compute_management_client
from azure.common.credentials import ServicePrincipalCredentials

credentials = ServicePrincipalCrendentials(client_id, secret, tenant)
client = compute_management_client(credentials, subscription_id)

client.virtual_machines.run_command(resource_group_name,
vm_name, parameters, customheaders=None, raw=False,
**operation_config)

但是我如何在这里传递命令呢?我找不到任何参数示例和操作配置。请帮忙

最佳答案

基本示例:

  run_command_parameters = {
'command_id': 'RunShellScript', # For linux, don't change it
'script': [
'ls /tmp'
]
}
poller = client.virtual_machines.run_command(
resource_group_name,
vm_name,
run_command_parameters
)
result = poller.result() # Blocking till executed
print(result.value[0].message) # stdout/stderr

如果你想注入(inject)参数,你可以这样做:

    run_command_parameters = {
'command_id': 'RunShellScript',
'script': [
'echo $arg1'
],
'parameters':[
{'name':"arg1", 'value':"hello world"}
]
}

如果使用 Windows,您可以使用 RunPowerShellScript 命令 ID

您可能想使用 CLI 测试您的命令:az vm run-command invoke --help

由于 CLI 使用此 SDK,因此您将获得相同的行为。

关于azure - 使用 python sdk 在 azure 中的 Linux 虚拟机中运行命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51478227/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com