Dns.GetHostAddress(hostname) No such device or address Ubuntu 16.04 中的异常

Posted

技术标签:

【中文标题】Dns.GetHostAddress(hostname) No such device or address Ubuntu 16.04 中的异常【英文标题】:Dns.GetHostAddress(hostname) No such device or address Exception in Ubuntu 16.04 【发布时间】:2018-10-26 04:16:37 【问题描述】:

我有一个 .net core 2.0 项目,它试图从 linux 机器上获取主机名和主机 IP 地址。该程序在 Mac OS 和 Windows 上运行良好,但在 linux - Ubuntu 16.04 上运行不佳

public class Program

    public static void Main(string[] args)
    
        var HostName = Dns.GetHostName();
        Console.WriteLine("Host name : " + HostName);
        var HostAddress = GetHostAddress(HostName);
        Console.WriteLine("Host address : " + HostAddress);
    

    private static string GetHostAddress(string hostName)
    
        try
        
            var addressList = Dns.GetHostAddresses(hostName);

            foreach (IPAddress address in addressList)
            
                Console.WriteLine("IP Address : " + address.ToString());
                if (address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
                
                    string ip = address.ToString();
                    if (!ip.StartsWith("127."))
                        return ip;
                
            

            return "127.0.0.1";
        
        catch (Exception e)
        
            Console.WriteLine(e);
            throw;
        
        
    

我得到的例外是

ubuntu@ip-10-40-121-185:~/home/IP$ dotnet IP.dll

主机名:ip-10-40-121-185

System.Net.Internals.SocketExceptionFactory+ExtendedSocketException (0x00000005):没有这样的设备或地址

在 System.Net.Dns.InternalGetHostByName(String hostName, Boolean includeIPv6)

在 System.Net.Dns.GetHostAddresses(String hostNameOrAddress)

在/Users/jliu/RiderProjects/IpTest/IP/Program.cs:line 34 中的 IP.Program.GetHostAddress(String hostName)

未处理的异常: System.Net.Internals.SocketExceptionFactory+ExtendedSocketException:没有这样的设备或地址

在 System.Net.Dns.InternalGetHostByName(String hostName, Boolean includeIPv6)

在 System.Net.Dns.GetHostAddresses(String hostNameOrAddress)

在/Users/jliu/RiderProjects/IpTest/IP/Program.cs:line 52 中的 IP.Program.GetHostAddress(String hostName)

在/Users/jliu/RiderProjects/IpTest/IP/Program.cs:line 20 中的 IP.Program.Main(String[] args) 中止(核心转储)

知道如何修复它或在 linux 机器上获取 IP 的任何替代方法吗? 谢谢。

【问题讨论】:

【参考方案1】:

从另一个对我有用的线程Get local IP address 获得答案

感谢@Gerardo H https://***.com/a/28621250/4861127 提供的解决方案

    internal static string GetLocalIPv4(NetworkInterfaceType _type)
    
        string output = "";
        foreach (NetworkInterface item in NetworkInterface.GetAllNetworkInterfaces())
        
            if (item.NetworkInterfaceType == _type && item.OperationalStatus == OperationalStatus.Up)
            
                IPInterfaceProperties adapterProperties = item.GetIPProperties();

                if (adapterProperties.GatewayAddresses.FirstOrDefault() != null)
                
                    foreach (UnicastIPAddressInformation ip in adapterProperties.UnicastAddresses)
                    
                        if (ip.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
                        
                            output = ip.Address.ToString();
                        
                    
                
            
        

        return output;
    

【讨论】:

以上是关于Dns.GetHostAddress(hostname) No such device or address Ubuntu 16.04 中的异常的主要内容,如果未能解决你的问题,请参考以下文章

C#获取本地IP地址

高可用pcs

logback

芹菜任务未处理

ab网站压力测试

Ubuntu18.04修改Hostname