2017-04-18 112 views
0

對於特定的VM,我希望能夠檢索公共IP地址。如何使用Azure SDK獲取Azure VM的公共IP

我知道如何獲取資源組的所有公共IP地址,我也知道如何獲得特定VM的nic-id - 但我無法弄清楚如何連接這兩個。

這是我有:

var resourceGroupName = "My-Resource-Group"; 
var vmName = "MyVM"; 
var subscriptionId = "bzz-bzz-bzz-bzz-bzz-bzz"; 

var tenantId = "bar-bar-bar-bar-bar-bar"; 

string clientId = "foo-foo-foo-foo-foo-foo"; 
string clientSecret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; 

var token = GetAccessTokenAsync(tenantId, clientId, clientSecret); 
var credential = new TokenCredentials(token.Result.AccessToken); 

var computeManagementClient = new ComputeManagementClient(credential) { SubscriptionId = subscriptionId }; 

var vmResult = await computeManagementClient.VirtualMachines.GetAsync(resourceGroupName, vmName, InstanceViewTypes.InstanceView); 

//Get the NIC ID for the VM: 

foreach (NetworkInterfaceReference nic in vmResult.NetworkProfile.NetworkInterfaces) 
     { 
      Console.WriteLine(" networkInterface id: " + nic.Id); 
     }  

這給了我這樣的事情:

/subscriptions/[guid]/resourceGroups/My-Resource-Group/providers/Microsoft.Network/networkInterfaces/myvm123

要獲取資源組所有的公網IP,我能做到這一點:

using (var client = new NetworkManagementClient(credential)) 
     { 
      client.SubscriptionId = subscriptionId; 
      foreach (var publicIpAddress in client.PublicIPAddresses.ListAll()) 
      { 
       Console.WriteLine(publicIpAddress.IpAddress); 
      } 
} 

...但檢查nic-id和公共ip對象的屬性,沒有明顯的方法從一個到另一個。

問:

如何從NIC-ID字符串得到的,實際的公共IP地址爲VM /網卡?

輔助功能:

private static async Task<AuthenticationResult> GetAccessTokenAsync(string tenantId, string clientId, string clientSecret) 
    { 
     var cc = new ClientCredential(clientId, clientSecret); 
     var context = new AuthenticationContext($"https://login.windows.net/{tenantId}"); 
     var token = context.AcquireToken("https://management.azure.com/", cc); 

     if (token == null) 
     { 
      throw new InvalidOperationException("Could not get the token"); 
     } 
     return token; 
    } 
+0

你看了看ComputeManagementClient.Deployments,然後你就可以獲得具有PublicIPs屬性的RoleInstance – Sorceri

+0

@Sorceri我已經可以獲取公有IP了 - 我只是不知道如何獲得具體的名稱VM。 – Kjensen

+0

Deployment.RoleInstances [index]是虛擬機,Deployment.RoleInstances [index] .InstanceName是虛擬機的名稱,deployment.RoleInstances [index] .PublicIPs是與該虛擬機關聯的公共IP。希望有幫助 – Sorceri

回答

1

我找到了一個解決方法。不漂亮,但它的作品。

它假定你已經有了一個Microsoft.Azure.Management.Compute.Models.VirtualMachine對象從這樣的事情:

VirtualMachine vmResult = await computeManagementClient.VirtualMachines.GetAsync(resourceGroupName, vmName, InstanceViewTypes.InstanceView); 

那麼你可以採取的第一個NIC,得到的是最後一部分作爲ID:

 var firstNic = vmResult.NetworkProfile.NetworkInterfaces.First(); 

     var nicNameParts = firstNic.Id.Split('/'); 
     string networkIntefaceName = nicNameParts.Last(); 

     using (var client = new NetworkManagementClient(credential)) 
     { 
      client.SubscriptionId = subscriptionId; 

      string publicNicId = string.Empty; 

      //Query ALL Networkinterfaces in the client, and find the one with the matching NIC-name 

      var nic = client.NetworkInterfaces.ListAll().FirstOrDefault(x => x.Name == networkIntefaceName); 

      if (nic != null) 
      { 
       //If we find that, we can now use that to find the ID of the PublicIPAddress for said NIC 

       publicNicId = nic.IpConfigurations[0].PublicIPAddress.Id; 
       //...And when we have that, we can now query all public IP addresses for that specific public Nic ID 
       var publicIp = client.PublicIPAddresses.ListAll().FirstOrDefault(x => x.Id == publicNicId); 
       if (publicIp != null) 
       { 
        vmInfo.PublicIP = publicIp.IpAddress; 
        Console.WriteLine(" public ip: " + publicIp.IpAddress); 
       } 
       else 
       { 
        Console.WriteLine(" public ip: unknown"); 
       } 
      } 
     } 

是的,它不是超級優雅,它可以優化等 - 但它的工作,所以這是一個開始。 :)

+0

我有你的解決方案沒有成功。我使用微軟的這種方法從VM獲取信息https://docs.microsoft.com/en-us/azure/virtual-machines/windows/csharp –