Get public/external IP address?

前端 未结 26 2328
鱼传尺愫
鱼传尺愫 2020-11-22 14:32

I cant seem to get or find information on finding my routers public IP? Is this because it cant be done this way and would have to get it from a website?

26条回答
  •  情书的邮戳
    2020-11-22 14:52

    Answers based on using external web services are not exactly correct, because they do not actually answer the stated question:

    ... information on finding my routers public IP


    Explanation

    All online services return the external IP address, but it does not essentially mean, that this address is assigned to the user's router.

    Router may be assigned with another local IP address of ISP infrastructure networks. Practically this means, that router can not host any services available on Internet. This may be good for safety of most home users, but not good for geeks who host servers at home.

    Here's how to check if router has external IP:

    According to Wikipedia article, the IP address ranges 10.0.0.0 – 10.255.255.255, 172.16.0.0 – 172.31.255.255 and 192.168.0.0 – 192.168.255.255 are used for private i.e. local networks.

    See what happens when you trace route to some remote host with router being assigned with external IP address:

    Gotcha! First hop starts from 31.* now. This clearly means that there's nothing between your router and Internet.


    Solution

    1. Make Ping to some address with Ttl = 2
    2. Evaluate where response comes from.

    TTL=2 must be not enough to reach remote host. Hop #1 host will emit "Reply from : TTL expired in transit." revealing its IP address.

    Implementation

    try
    {
        using (var ping = new Ping())
        {
            var pingResult = ping.Send("google.com");
            if (pingResult?.Status == IPStatus.Success)
            {
                pingResult = ping.Send(pingResult.Address, 3000, "ping".ToAsciiBytes(), new PingOptions { Ttl = 2 });
    
                var isRealIp = !Helpers.IsLocalIp(pingResult?.Address);
    
                Console.WriteLine(pingResult?.Address == null
                    ? $"Has {(isRealIp ? string.Empty : "no ")}real IP, status: {pingResult?.Status}"
                    : $"Has {(isRealIp ? string.Empty : "no ")}real IP, response from: {pingResult.Address}, status: {pingResult.Status}");
    
                Console.WriteLine($"ISP assigned REAL EXTERNAL IP to your router, response from: {pingResult?.Address}, status: {pingResult?.Status}");
            }
            else
            {
                Console.WriteLine($"Your router appears to be behind ISP networks, response from: {pingResult?.Address}, status: {pingResult?.Status}");
            }
        }
    }
    catch (Exception exc)
    {
        Console.WriteLine("Failed to resolve external ip address by ping");
    }
    

    Small helper is used to check if IP belongs to private or public network:

    public static bool IsLocalIp(IPAddress ip) {
        var ipParts = ip.ToString().Split(new [] { "." }, StringSplitOptions.RemoveEmptyEntries).Select(int.Parse).ToArray();
    
        return (ipParts[0] == 192 && ipParts[1] == 168) 
            || (ipParts[0] == 172 && ipParts[1] >= 16 && ipParts[1] <= 31) 
            ||  ipParts[0] == 10;
    }
    

提交回复
热议问题