[英]How to check if a virtual machine is running using Python
我正在編寫一個Python Runbook,以能夠啟動或停止虛擬機。 如果虛擬機正在運行,我想停止它。 如果它沒有運行,我想打開它。 我需要編寫一個if條件來執行此操作,但是我不知道如何以天藍色獲取虛擬機的狀態,因此無法進行比較。
我嘗試使用以下內容:
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狀態即可。 下面的代碼:
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)
輸出在這里:
有關更多詳細信息,請參閱VM信息中的instance_view 。
或者,您也可以直接獲取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) 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.