Find the start and end IP addresses of a network segment

WBOY
Release: 2016-07-25 08:50:51
Original
1799 people have browsed it
Find the starting and ending IP addresses of a network segment
For example: network segment (192168.1.5/24), its subnet mask is divided according to 24:
11111111.11111111.11111111.00000000(255.255.255.0)
Explanation: The IP address is 32bits, In dividing the network segment, 24 means that there are 24 1s in the front and 8 0s in the back. The algorithm of the starting IP address is: the binary of 192.168.1.5 is ANDed with the binary of the subnet mask.
The algorithm for ending the IP address is: first invert the binary number of the subnet mask, and then perform an "OR" operation with the binary number of 192.168.1.5
In actual application, what is obtained is the network address and broadcast address, network address + 1 is the first host address, and the broadcast address -1 is the last host address.
    function mask2bin($n)
  1. {
  2. $n = intval($n);
  3. if($n<0||$n>32)
  4. die('error submask');
  5. return str_repeat("1", $n).str_repeat("0",32-$n);
  6. }
  7. function revBin($s)
  8. {
  9. $p=array('0','1',' 2');
  10. $r=array('2','0','1');
  11. return str_replace($p,$r,$s);
  12. }
  13. function startIp($str,$bSub )
  14. {
  15. $bIp = decbin($str);
  16. $bIp = str_pad($bIp,8,"0",STR_PAD_LEFT);
  17. $sIp = bindec($bIp & $bSub);
  18. return $sIp;
  19. }
  20. function endIp($str,$bSub)
  21. {
  22. $bIp = decbin($str);
  23. $bIp = str_pad($bIp,8,"0",STR_PAD_LEFT);
  24. $eIp = bindec($bIp | revBin($bSub));
  25. return $eIp;
  26. }
  27. $ip = array('192','168','1','5');//Set the IP address, which can be obtained from the form, This is just for demonstration
  28. $mask = '24'; //Set the mask
  29. $bSub = mask2bin($mask); //Convert the subnet mask to binary
  30. $mask = array();
  31. $mask[] = substr($bSub,"0",8); //Divide the subnet mask into segments every 8 bits
  32. $mask[] = substr($bSub,"8",8);
  33. $mask[] = substr ($bSub,"16",8);
  34. $mask[] = substr($bSub,"24",8);
  35. echo '
  36. < ;td>
  37. ';
  38. for ($i=0;$i<4;$i++)
  39. {
  40. echo bindec($mask[$i]);
  41. if( $i!=3)
  42. echo ".";
  43. }
  44. echo '
  45. Mask:
    Network address:
  46. ';
  47. for ($i=0 ;$i<4;$i++)
  48. {
  49. echo startIp($ip[$i],$mask[$i]);
  50. if($i!=3)
  51. echo ".";
  52. }
  53. echo '
  54. First available:
  55. ';
  56. for ($i=0;$i< ;3;$i++)
  57. {
  58. echo startIp($ip[$i],$mask[$i]);
  59. echo ".";
  60. }
  61. $ip_4 = startIp($ip[3],$mask[ 3]);
  62. echo ++$ip_4;
  63. echo '
  64. < font size="2">Last available:
  65. ';
  66. for ($i=0;$i< ;3;$i++)
  67. {
  68. echo endIp($ip[$i],$mask[$i]);
  69. echo ".";
  70. }
  71. $ip_4 = endIp($ip[3],$mask[ 3]);
  72. echo --$ip_4;
  73. echo '
  74. < font size="2">Broadcast address:
  75. ';
  76. for ($i=0;$i< ;4;$i++)
  77. {
  78. echo endIp($ip[$i],$mask[$i]);
  79. if($i!=3)
  80. echo ".";
  81. }
  82. ?>
  83. Copy code

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!