使用Python SDK在Azure中的Linux VM中运行命令 [英] Run command in linux vm in azure using python sdk

查看:79
本文介绍了使用Python SDK在Azure中的Linux VM中运行命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现azure python sdk提供了以下用于在Linux vm中运行命令的方法.

I have found that azure python sdk has provided following method for running command in 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.请帮助

But how do I pass my command here? I couldn't find any sample for parameters and operation_config. Please Help

推荐答案

基本示例:

  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

如果要注入参数,可以执行以下操作:

If you wish to inject parameters, you can do this:

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

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

If using Windows, you can use RunPowerShellScript command id

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

You might want to test your command using the CLI: az vm run-command invoke --help

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

Since the CLI is using this SDK, you'll get the same behavior.

这篇关于使用Python SDK在Azure中的Linux VM中运行命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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