Find the start and end IP addresses of a CIDR block

Source: Internet
Author: User
Find the start and end IP addresses of a CIDR block
Find the start and end IP addresses of a CIDR block
For example, for a network segment (192168.1.5/24), its subnet mask is divided:
11111111.11111111.11111111.00000000 (255.255.255.0)
Note: If the IP address is 32 bits, 24 indicates that there are 24 first 1 and 8 are later 0 in the network segment division.
The algorithm for starting an IP address is: the binary of 192.168.1.5 and the binary of the subnet mask.
The algorithm for ending an IP address is: the subnet mask is first reversed, and then "or" is performed with the 192.168.1.5 binary.
In practice, the network address and broadcast address are obtained as follows. network address + 1 is the first host address, and broadcast address-1 is the last host address.

  1. Function mask2bin ($ n)
  2. {
  3. $ N = intval ($ n );
  4. If ($ n <0 | $ n> 32)
  5. Die ('error submask ');
  6. Return str_repeat ("1", $ n). str_repeat ("0", 32-$ n );
  7. }
  8. Function revBin ($ s)
  9. {
  10. $ P = array ('0', '1', '2 ');
  11. $ R = array ('2', '0', '1 ');
  12. Return str_replace ($ p, $ r, $ s );
  13. }
  14. Function startIp ($ str, $ bSub)
  15. {
  16. $ BIp = decbin ($ str );
  17. $ BIp = str_pad ($ bIp, 8, "0", STR_PAD_LEFT );
  18. $ SIp = bindec ($ bIp & $ bSub );
  19. Return $ sIp;
  20. }
  21. Function endIp ($ str, $ bSub)
  22. {
  23. $ BIp = decbin ($ str );
  24. $ BIp = str_pad ($ bIp, 8, "0", STR_PAD_LEFT );
  25. $ EIp = bindec ($ bIp | revBin ($ bSub ));
  26. Return $ eIp;
  27. }
  28. $ Ip = array ('123', '123', '1', '5'); // Set the ip address, which can be obtained from the form. This example is used only for demonstration.
  29. $ Mask = '24'; // sets the mask.
  30. $ BSub = mask2bin ($ mask); // Convert the subnet mask to binary
  31. $ Mask = array ();
  32. $ Mask [] = substr ($ bSub, "0", 8); // divide the subnet mask into eight parts.
  33. $ Mask [] = substr ($ bSub, "8", 8 );
  34. $ Mask [] = substr ($ bSub, "16", 8 );
  35. $ Mask [] = substr ($ bSub, "24", 8 );
  36. Echo'
  37. Mask:
  38. ';
  39. For ($ I = 0; $ I <4; $ I ++)
  40. {
  41. Echo bindec ($ mask [$ I]);
  42. If ($ I! = 3)
  43. Echo ".";
  44. }
  45. Echo'
  46. Network address:
  47. ';
  48. For ($ I = 0; $ I <4; $ I ++)
  49. {
  50. Echo startIp ($ ip [$ I], $ mask [$ I]);
  51. If ($ I! = 3)
  52. Echo ".";
  53. }
  54. Echo'
  55. First available:
  56. ';
  57. For ($ I = 0; $ I <3; $ I ++)
  58. {
  59. Echo startIp ($ ip [$ I], $ mask [$ I]);
  60. Echo ".";
  61. }
  62. $ Ip_4 = startIp ($ ip [3], $ mask [3]);
  63. Echo ++ $ ip_4;
  64. Echo'
  65. Last available:
  66. ';
  67. For ($ I = 0; $ I <3; $ I ++)
  68. {
  69. Echo endIp ($ ip [$ I], $ mask [$ I]);
  70. Echo ".";
  71. }
  72. $ Ip_4 = endIp ($ ip [3], $ mask [3]);
  73. Echo -- $ ip_4;
  74. Echo'
  75. Broadcast address:
  76. ';
  77. For ($ I = 0; $ I <4; $ I ++)
  78. {
  79. Echo endIp ($ ip [$ I], $ mask [$ I]);
  80. If ($ I! = 3)
  81. Echo ".";
  82. }
  83. ?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.