亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用 azure sdk 獲取 vm 公共 IP?

如何使用 azure sdk 獲取 vm 公共 IP?

C#
UYOU 2022-06-18 16:25:42
我使用Microsoft.Azure.Compute.Fluentsdk 列出了我所有的虛擬機,它工作正常,除了我無法獲取公共 IP 地址:    IVirtualMachines _client = azure.VirtualMachines;     var list = await _client.ListAsync();    foreach (var instance in list)    {        var name = instance.Name;        var ip = instance.GetPrimaryPublicIPAddress().IPAddress;        //ip = null here;    }好吧,我嘗試了其他東西,但總是將公共 ip 設為空。如何正確檢索公共 IP?
查看完整描述

1 回答

?
鳳凰求蠱

TA貢獻1825條經驗 獲得超4個贊

如果我誤解了你,請糾正我:我找不到 sdk Microsoft.Azure.Compute.Fluent(截圖如下):

http://img1.sycdn.imooc.com//62ad8c200001736f11750312.jpg

所以我改用這個sdk Microsoft.Azure.Management.Fluent(官方文檔也使用它)。并且可以在我身邊獲取ip,代碼如下:


using Microsoft.Azure.Management.Compute.Fluent;

using Microsoft.Azure.Management.Fluent;

using Microsoft.Azure.Management.ResourceManager.Fluent;

using Microsoft.Azure.Management.ResourceManager.Fluent.Core;

using System;

using System.Threading.Tasks;


namespace myVMDotnetProject

{

    class Program

    {

        static void Main(string[] args)

        {

            GetVMInfo();


            Console.WriteLine("okok");

            Console.ReadLine();

        }



        static async Task  GetVMInfo()

        {

            var credentials = SdkContext.AzureCredentialsFactory.FromFile(Environment.GetEnvironmentVariable("AZURE_AUTH_LOCATION", EnvironmentVariableTarget.User));


            var azure = Azure

                .Configure()

                .WithLogLevel(HttpLoggingDelegatingHandler.Level.Basic)

                .Authenticate(credentials)

                .WithDefaultSubscription();


            IVirtualMachines _client = azure.VirtualMachines;

            var list = await _client.ListAsync();


            foreach (var instance in list)

            {

                var name = instance.Name;

                var ip = instance.GetPrimaryPublicIPAddress().IPAddress;

                Console.WriteLine("name: " + name + ", ip: " + ip);

            }

        }


    }

}

測試結果如下:

http://img1.sycdn.imooc.com//62ad8c34000107db16160874.jpg

查看完整回答
反對 回復 2022-06-18
  • 1 回答
  • 0 關注
  • 142 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號