gpt4 book ai didi

python - 如何使用 Python 检查虚拟机是否正在运行

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

我正在编写一个 python Runbook,以便能够启动或停止虚拟机。如果虚拟机正在运行,我想停止它。如果它没有运行,我想将其打开。我需要编写一个 if 条件来执行此操作,但我不知道如何在 azure 中获取虚拟机的状态,以便进行比较。

我尝试使用以下内容:

compute_client.virtual_machines.get(resourceGroupName, vmName, expand = 'instanceview')

但是当我打印此内容时,我不知道如何访问虚拟机的状态。

这是我的脚本代码:

import os
from azure.mgmt.compute import ComputeManagementClient
import azure.mgmt.resource
import automationassets

import sys

resourceGroupName = str(sys.argv[1])
vmName = str(sys.argv[2])

def get_automation_runas_credential(runas_connection):
from OpenSSL import crypto
import binascii
from msrestazure import azure_active_directory
import adal

# Get the Azure Automation RunAs service principal certificate
cert = automationassets.get_automation_certificate("AzureRunAsCertificate")
pks12_cert = crypto.load_pkcs12(cert)
pem_pkey = crypto.dump_privatekey(crypto.FILETYPE_PEM,pks12_cert.get_privatekey())

# Get run as connection information for the Azure Automation service principal
application_id = runas_connection["ApplicationId"]
thumbprint = runas_connection["CertificateThumbprint"]
tenant_id = runas_connection["TenantId"]

# Authenticate with service principal certificate
resource ="https://management.core.windows.net/"
authority_url = ("https://login.microsoftonline.com/"+tenant_id)
context = adal.AuthenticationContext(authority_url)
return azure_active_directory.AdalAuthentication(
lambda: context.acquire_token_with_client_certificate(
resource,
application_id,
pem_pkey,
thumbprint)
)

# Authenticate to Azure using the Azure Automation RunAs service principal
runas_connection = automationassets.get_automation_connection("AzureRunAsConnection")
azure_credential = get_automation_runas_credential(runas_connection)

# Initialize the compute management client with the RunAs credential and specify the subscription to work against.
compute_client = ComputeManagementClient(
azure_credential,
str(runas_connection["SubscriptionId"])
)

printMe = compute_client.virtual_machines.get(resourceGroupName, vmName, expand = 'instanceview')
print(printMe)

#Start the VM if not running:

print('\n' + 'Starting the ' + ' ' + vmName + ' ' + 'in ' + ' ' + resourceGroupName)
async_vm_start = compute_client.virtual_machines.start(
resourceGroupName, vmName)
async_vm_start.wait()



最佳答案

您使用的 Azure SDK azure.mgmt.compute 是正确的。您只需要获取该信息中的虚拟机状态即可。代码如下:

vm = compute_client.virtual_machines.get('v-chaxu-ChinaCXPTeam', 'azureUbuntu18', expand='instanceView')

# These are the statuses of the VM about the event execution status and the vm state, the vm state is the second one.
statuses = vm.instance_view.statuses
print(statuses[1].display_status)

这里的输出:

enter image description here

有关更多详细信息,请参阅instance_view in VM information .

或者你也可以直接获取instance_view,代码如下:

instance_view = compute_client.virtual_machines.instance_view('v-chaxu-ChinaCXPTeam', 'azureUbuntu18')
print(instance_view.statuses[1].display_status)

输出也与上面相同。更多详情请参见函数 instance_view(resource_group_name, vm_name, custom_headers=None, raw=False, **operation_config) .

关于python - 如何使用 Python 检查虚拟机是否正在运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57796635/

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