如何获取一个IP段内的所有IP地址

时间:2024-03-10 10:32:40

现在有这样一个场景,提供一个起始的IP地址,提供一个结束的IP地址,要循环出IP段内所有的IP地址。

 

当然首先想到就是4个for循环。也是最简单的做法。现在肯定不会这样用。

 

再想到是通过2个循环或者1个函数调用来实现,因为对二进制的加减操作不熟悉,没有结果。

 

后来参考网络上的方法,出了1个个人认为较好的解决方案。

 

View Code
 1 using System;
2 using System.Collections;
3
4 namespace ConsoleApplicationForTest
5 {
6 class Program
7 {
8 static void Main(string[] args)
9 {
10 string startIP = "10.1.1.1";
11 string endIP = "10.2.3.4";
12
13 long lsIP = ToHex(startIP);
14 long leIP = ToHex(endIP);
15
16 SortedList list = new SortedList();
17
18 for (; lsIP < leIP; lsIP++)
19 {
20 string temp = ToIp(lsIP);
21 list.Add(temp, temp);
22 }
23 }
24
25 /// <summary>
26 /// 将IP地址转换成数值形式
27 /// </summary>
28 /// <param name="ip"></param>
29 /// <returns>长整形</returns>
30 public static long ToHex(string ip)
31 {
32 string[] arrIP = ip.Split(\'.\');
33 for (int i = 0; i < arrIP.Length; i++)
34 {
35 arrIP[i] = int.Parse(arrIP[i]) > 255 ? Convert.ToString(255).PadLeft(2, \'0\') : arrIP[i] = Convert.ToString(int.Parse(arrIP[i]), 16).PadLeft(2, \'0\');
36 }
37 ip = string.Join("", arrIP);
38
39 long hexIP = long.Parse(ip, System.Globalization.NumberStyles.HexNumber);
40 return hexIP;
41 }
42
43 /// <summary>
44 /// 将数值形式转换成IP地址
45 /// </summary>
46 /// <param name="l"></param>
47 /// <returns>IP地址</returns>
48 public static string ToIp(long l)
49 {
50 var bt = new long[4];
51 for (int i = 0; i < 4; i++)
52 {
53 bt[3 - i] = (long)(l >> 8 * i & 255);
54 }
55
56 string ipAddress = string.Join(".", bt);
57 return ipAddress;
58 }
59 }
60 }


下面这个是同事给的一个Class

 

View Code
 1 using System;
2 using System.Net;
3
4 namespace ConsoleApplicationForTest
5 {
6 public class IpDigitCovert
7 {
8 /// <summary>
9 /// 将IP地址转为数值形式
10 /// </summary>
11 /// <returns>长整数</returns>
12 public static long ToDigit(IPAddress ip)
13 {
14 int x = 3;
15 long l = 0;
16 foreach (byte b in ip.GetAddressBytes())
17 {
18 l += (long)b << 8 * x;
19 }
20 return l;
21 }
22
23 /// <summary>
24 /// 将数值转换为IP地址
25 /// </summary>
26 /// <returns>IP地址</returns>
27 public static IPAddress ToIP(long l)
28 {
29 var bt = new byte[4];
30 for (int i = 0; i < 4; i++)
31 {
32 bt[3 - i] = (byte)(l >> 8 * i & 255);
33 }
34 return new IPAddress(bt);
35 }
36
37 /// <summary>
38 /// 将IP地址转为数值形式
39 /// </summary>
40 /// <param name="ip"></param>
41 /// <returns>长整数</returns>
42 public static long ToHex(string ip)
43 {
44 string[] arrIP = ip.Split(\'.\');
45 for (int i = 0; i < arrIP.Length; i++)
46 {
47 arrIP[i] = int.Parse(arrIP[i]) > 255 ? Convert.ToString(255).PadLeft(2, \'0\') : arrIP[i] = Convert.ToString(int.Parse(arrIP[i]), 16).PadLeft(2, \'0\');
48 }
49 ip = string.Join("", arrIP);
50
51 long hexIP = long.Parse(ip, System.Globalization.NumberStyles.HexNumber);
52 return hexIP;
53 }
54
55 /// <summary>
56 /// 将数值形式转为IP地址
57 /// </summary>
58 /// <param name="l"></param>
59 /// <returns>ip地址</returns>
60 public static string ToIp(long l)
61 {
62 var bt = new long[4];
63 for (int i = 0; i < 4; i++)
64 {
65 bt[3 - i] = (long)(l >> 8 * i & 255);
66 }
67 string ipAddress = string.Join(".", bt);
68 return ipAddress;
69 }
70
71 }
72 }