how to determine whether an IP address in private?

后端 未结 5 1918
不知归路
不知归路 2020-12-09 02:30

So far I have this code:

NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces();

foreach (NetworkInterface adapter in adapters)
{
  IPInte         


        
5条回答
  •  醉酒成梦
    2020-12-09 03:35

    10.0.0.0        -   10.255.255.255  (10/8 prefix)
    172.16.0.0      -   172.31.255.255  (172.16/12 prefix)
    192.168.0.0     -   192.168.255.255 (192.168/16 prefix)
    

    Use the ranges defined in the RFC (as suggested by Anders); than use regular expression to detect/remove the private IP address from the list.

    Here is a sample RegEx to detect private IP addresses. (Not tested by me)

    (^127\.0\.0\.1)|
    (^10\.)|
    (^172\.1[6-9]\.)|(^172\.2[0-9]\.)|(^172\.3[0-1]\.)|
    (^192\.168\.)
    

提交回复
热议问题