Verification: javascript checks whether two IP addresses are in the same CIDR block.

Source: Internet
Author: User
1) Basic Idea: Determine whether two IP addresses are in the same CIDR block, and separate the IP addresses with the subnet mask. The result is the network number. If the network number is the same, it is in the same subnet. Otherwise, it is not in the same subnet. 2) Implementation: *** [isEqualIPAddres 1) Basic Idea: Determine whether two IP addresses are in the same CIDR block, and perform and calculate the IP addresses and subnet masks respectively, the result is the network number. If the network number is the same, it is in the same subnet. Otherwise, it is not in the same subnet. 2) specific implementation:
/*** [IsEqualIPAddress determines whether two IP addresses are in the same CIDR block] * @ param {[String]} addr1 [address 1] * @ param {[String]} addr2 [address 2] * @ param {[String]} mask [subnet mask] * @ return {Boolean} [true or false] */function isEqualIPAddress (addr1, addr2, mask) {if (! Addr1 |! Addr2 |! Mask) {console. log ("parameters cannot be blank"); return false;} var res1 = [], res2 = []; addr1 = addr1.split (". "); addr2 = addr2.split (". "); mask = mask. split (". "); for (var I = 0, ilen = addr1.length; I <ilen; I + = 1) {res1.push (parseInt (addr1 [I]) & parseInt (mask [I]); res2.push (parseInt (addr2 [I]) & parseInt (mask [I]);} if (res1.join (". ") = res2.join (". ") {console. log ("in the same network segment"); return true;} else {console. log ("not in the same network segment"); return false ;}}

Related Article

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.