Sur*_*tha 4 azure azure-virtual-machine azure-sdk-python
我发现 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)
Run Code Online (Sandbox Code Playgroud)
但是我如何在这里传递我的命令?我找不到参数和 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 Code Online (Sandbox Code Playgroud)
如果你想注入参数,你可以这样做:
run_command_parameters = {
'command_id': 'RunShellScript',
'script': [
'echo $arg1'
],
'parameters':[
{'name':"arg1", 'value':"hello world"}
]
}
Run Code Online (Sandbox Code Playgroud)
如果使用 Windows,则可以使用RunPowerShellScript命令 id
您可能想使用 CLI 测试您的命令: az vm run-command invoke --help
由于 CLI 正在使用此 SDK,您将获得相同的行为。
| 归档时间: |
|
| 查看次数: |
2159 次 |
| 最近记录: |