Start and end IP addresses for a network segment

Source: Internet
Author: User
Start and end IP addresses for a network segment
For example: Network segment (192168.1.5/24), whose subnet mask is divided according to 24:
11111111.11111111.11111111.00000000 (255.255.255.0)
Description: The IP address is 32bits,24 in the division of the network segment is indicated in front of 24 1, followed by 8 0.
The algorithm for starting the IP address is: The binary of the 192.168.1.5 and the subnet mask binary are "with" the Operation out.
The algorithm for ending an IP address is: The binary of the subnet mask is first reversed, and then the "or" operation is performed with the binary of the 192.168.1.5
The actual application, that is, the network address and broadcast address, network address +1 is the first host address, 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 (' 192 ', ' 168 ', ' 1 ', ' 5 ');//Set IP address, which can be obtained from the form, only for demonstration
  29. $mask = ' 24 '; Set Mask
  30. $bSub = Mask2bin ($mask); To convert a subnet mask into binary
  31. $mask = Array ();
  32. $mask [] = substr ($bSub, "0", 8); Divide the subnet mask every 8 bits
  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. The first one is 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. ?> Copy Code

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.