我们可以使用Azure Java SDK做到这一点。有关身份验证,请参阅使用适用于 Java 的 Azure 管理库进行身份验证。
以下是演示代码,我自己测试过。
String client = "clientId";
String tenant = "tenantId";
String key = "scret key";
ApplicationTokenCredentials credentials = new ApplicationTokenCredentials(client,
tenant,
key,
AzureEnvironment.AZURE);
Azure azure = Azure.configure().authenticate(credentials).withDefaultSubscription();
List <VirtualMachine> vmlist = azure.virtualMachines().list();
List<String> networkInterfaceList = new ArrayList<String>();
PagedList<NetworkSecurityGroup> list = azure.networkSecurityGroups().listByResourceGroup("resourceGroupName");
//add the networkinterface to the list
for (NetworkSecurityGroup nsg :list
) {
for (String networkInterface :nsg.networkInterfaceIds()
) {
networkInterfaceList.add(networkInterface);
}
};
List<VirtualMachine> virtualMachineList = new ArrayList<VirtualMachine>();
for (VirtualMachine vm :vmlist
) {
for (String vmInterface:vm.networkInterfaceIds()
) {
if(networkInterfaceList.contains(vmInterface))
{
if(!virtualMachineList.contains(vm))
{
virtualMachineList.add(vm);
System.out.println(vm.computerName());
}
}
}
}