如何获取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,可以把查询语句改成:
这样会把所有私有IP用逗号分隔展示。--query "[].{Name: name, ResourceGroup: resourceGroup, ID: id, PrivateIPs: join(', ', networkProfile.networkInterfaces[].ipConfigurations[].privateIpAddress)}" -o table
验证说明
运行修改后的脚本,输入目标资源组名称后,就能看到包含私有IP的虚拟机信息列表了。
内容的提问来源于stack exchange,提问作者Afshaar Ahmed
相关产品推荐
相关产品推荐

