How to get *internet* IP?

后端 未结 15 2101
刺人心
刺人心 2020-11-29 02:57

Imagine a situation, I have PC with two lan cards, one is connected to internet another is connected to local network, how can I detect IP which is connected to internet wi

15条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-29 03:52

    in the same direction as Lukas Šalkauskas, a friend of mine and I wrote this code:

       /* digged, built and (here and there) copied "as is" from web, without paying attention to style.
        Please, do not shot us for this. 
       */
    public static void DisplayIPAddresses() 
        { 
    
            Console.WriteLine("\r\n****************************"); 
            Console.WriteLine("     IPAddresses"); 
            Console.WriteLine("****************************"); 
    
    
            StringBuilder sb = new StringBuilder(); 
            // Get a list of all network interfaces (usually one per network card, dialup, and VPN connection)      
            NetworkInterface[] networkInterfaces = NetworkInterface.GetAllNetworkInterfaces(); 
    
            foreach (NetworkInterface network in networkInterfaces) 
            { 
    
                if (network.OperationalStatus == OperationalStatus.Up ) 
                { 
                    if (network.NetworkInterfaceType == NetworkInterfaceType.Tunnel) continue; 
                    if (network.NetworkInterfaceType == NetworkInterfaceType.Tunnel) continue; 
                    //GatewayIPAddressInformationCollection GATE = network.GetIPProperties().GatewayAddresses; 
                    // Read the IP configuration for each network    
    
                    IPInterfaceProperties properties = network.GetIPProperties(); 
                    //discard those who do not have a real gateaway  
                    if (properties.GatewayAddresses.Count > 0) 
                    { 
                        bool good = false; 
                        foreach  (GatewayIPAddressInformation gInfo in properties.GatewayAddresses) 
                        { 
                            //not a true gateaway (VmWare Lan) 
                            if (!gInfo.Address.ToString().Equals("0.0.0.0")) 
                            { 
                                sb.AppendLine(" GATEAWAY "+ gInfo.Address.ToString()); 
                                good = true; 
                                break; 
                            } 
                        } 
                        if (!good) 
                        { 
                            continue; 
                        } 
                    } 
                    else { 
                        continue; 
                    } 
                    // Each network interface may have multiple IP addresses        
                    foreach (IPAddressInformation address in properties.UnicastAddresses) 
                    { 
                        // We're only interested in IPv4 addresses for now        
                        if (address.Address.AddressFamily != AddressFamily.InterNetwork) continue; 
    
                        // Ignore loopback addresses (e.g., 127.0.0.1)     
                        if (IPAddress.IsLoopback(address.Address)) continue; 
    
                        if (!address.IsDnsEligible) continue; 
                        if (address.IsTransient) continue;  
    
                        sb.AppendLine(address.Address.ToString() + " (" + network.Name + ") nType:" + network.NetworkInterfaceType.ToString()     ); 
                    } 
                } 
            } 
            Console.WriteLine(sb.ToString()); 
        }
    

提交回复
热议问题