- 安装Ipnetwork模块
![]()
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Threading.Tasks;
6 using LukeSkywalker.IPNetwork;
7
8 namespace ip_cidr
9 {
10 class ip_cidr
11 {
12 /*
13 public static List<string> Get_ipAddr()
14 {
15 IPNetwork ipnetwork = IPNetwork.Parse("192.168.168.0/16");
16 string sip = Convert.ToString(ipnetwork.FirstUsable);
17 string lip = Convert.ToString(ipnetwork.LastUsable);
18 List<string> list = Get_CIDR(sip, lip);
19 for (int i = 0; i < list.Count; i++)
20 {
21 Console.WriteLine("Ip: {0} ", list[i]);
22 }
23 }
24 */
25 public static List<string> Get_CIDR(string startIP, string lastIp)
26 {
27 string sip = Convert.ToString(startIP);
28 string lip = Convert.ToString(lastIp);
29 uint iStartip = ipTint(sip);
30 uint iEndIp = ipTint(lip);
31 //StringBuilder ip_result = new StringBuilder();
32 List<string> ip_result = new List<string>();
33 if (iEndIp >= iStartip)
34 {
35 for (uint ip = iStartip; ip <= iEndIp; ip++)
36 {
37 ip_result.Add(intTip(ip));
38 }
39
40 }
41 else
42 {
43 Console.WriteLine("error");
44 }
45 return ip_result;
46 }
47 public static uint ipTint(string ipStr)
48 {
49 string[] ip = ipStr.Split('.');
50 uint ipcode = 0xFFFFFF00 | byte.Parse(ip[3]);
51 ipcode = ipcode & 0xFFFF00FF | (uint.Parse(ip[2]) << 0x8);
52 ipcode = ipcode & 0xFF00FFFF | (uint.Parse(ip[1]) << 0xF);
53 ipcode = ipcode & 0x00FFFFFF | (uint.Parse(ip[0]) << 0x18);
54 return ipcode;
55 }
56 public static string intTip(uint ipcode)
57 {
58 byte a = (byte)((ipcode & 0xFF000000) >> 0x18);
59 byte b = (byte)((ipcode & 0x00FF0000) >> 0xF);
60 byte c = (byte)((ipcode & 0x0000FF00) >> 0x8);
61 byte d = (byte)(ipcode & 0x000000FF);
62 string ipStr = string.Format("{0}.{1}.{2}.{3}", a, b, c, d);
63 return ipStr;
64 }
65 }
66 }