You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何获取Azure虚拟机的私有IP?现有Bash脚本无法实现

解决Azure虚拟机私有IP获取问题

你的现有脚本可以通过调整Azure CLI的查询语句,新增私有IP字段的获取。以下是修改后的完整脚本:

#!/bin/bash

echo "Enter the names of the resource groups separated by spaces: "
read -a resource_groups

# Loop through each resource group and retrieve information about its Azure VMs
for resource_group in "${resource_groups[@]}"; do
  echo "Getting information for resource group: $resource_group"
  # 调整query参数,新增私有IP字段
  vm_info=$(az vm list --resource-group $resource_group --query "[].{Name: name, ResourceGroup: resourceGroup, ID: id, PrivateIP: networkProfile.networkInterfaces[0].ipConfigurations[0].privateIpAddress}" -o table)

  echo "Here is the information about the Azure VMs in resource group '$resource_group':"
  echo "$vm_info"
  echo ""
done

关键修改说明

  • 核心是修改--query参数,添加PrivateIP: networkProfile.networkInterfaces[0].ipConfigurations[0].privateIpAddress,这个路径指向虚拟机第一个网卡的第一个IP配置中的私有IP地址。
  • 如果你的虚拟机存在多个网卡或多个IP配置,想要列出所有私有IP,可以把查询语句改成:
    --query "[].{Name: name, ResourceGroup: resourceGroup, ID: id, PrivateIPs: join(', ', networkProfile.networkInterfaces[].ipConfigurations[].privateIpAddress)}" -o table
    
    这样会把所有私有IP用逗号分隔展示。

验证说明

运行修改后的脚本,输入目标资源组名称后,就能看到包含私有IP的虚拟机信息列表了。

内容的提问来源于stack exchange,提问作者Afshaar Ahmed

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.01 05:40:30