运行 命令在 linux Azure 中的 vm 使用 python sdk
Run command in linux vm in azure using python sdk
我发现 azure python sdk 为 linux vm 中的 运行 命令提供了以下方法。
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)
但是我如何在这里传递我的命令呢?我找不到任何参数示例
和 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
如果你想注入参数,你可以这样做:
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 为 linux vm 中的 运行 命令提供了以下方法。
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)
但是我如何在这里传递我的命令呢?我找不到任何参数示例 和 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
如果你想注入参数,你可以这样做:
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,您将获得相同的行为。