Lib to calc Variable Length Subnet Masking
npm install vlsmcalc
vlsmcalc is a library designed for network administrators and developers to easily calculate Variable Length Subnet Masking (VLSM). It helps you generate optimized subnets for a given major network, considering specific host requirements for each subnet.
---
1. Features
2. Prerequisites
3. Installation
4. Usage
5. Examples
- Subnetting Example
- Calculating Next Network
6. Contributing
7. License
Before using vlsmcalc, ensure that you have:
- Node.js (version 12 or higher) installed. Download Node.js
- A package manager such as npm or yarn:
- To install npm, download Node.js from nodejs.org.
- To install yarn, follow the instructions on Yarn's website.
To install the library, you can use Yarn or npm:
Using Yarn:
``bash`
yarn add vlsmcalc`
Using npm:bash`
npm install vlsmcalc
To get started with local development, follow these steps:
1. Clone the repository:
`bash`
git clone https://github.com/yourusername/project-name.git`
2. Install the project dependencies:bash`
yarn install`
3. Run the tests:bash`
yarn test
Here's an example of how to use vlsmcalc:
`javascript
const Subnet = require('vlsmcalc');
// Define the major network and hosts per subnet
const majorNetwork = '192.168.1.0/24';
const hostsEachSubnet = [50, 20, 10];
// Instantiate the Subnet class with host requirements and major network
const subnetCalc = new Subnet(hostsEachSubnet, majorNetwork);
// Validate if subnetting is possible
if (subnetCalc.isValid()) {
const networks = subnetCalc.getNetworks();
networks.forEach((network, index) => {
console.log(Subnet ${index + 1}:); Network Address: ${network.getNetwork()}
console.log(); Subnet Mask: ${network.getSubnetMask()}
console.log(); Broadcast Address: ${network.getBroadcast()}
console.log(); First Usable IP: ${network.getFirstIP()}
console.log(); Last Usable IP: ${network.getLastIP()}
console.log(); Prefix: /${network.getPrefix()}
console.log(); Allocated Size: ${network.getAllocatedSize()} hosts
console.log();
});
} else {
console.log('Invalid major network or insufficient space for requested subnets.');
}
`
For improved reliability, you can include error handling to catch invalid inputs or unexpected issues:
`javascript`
try {
if (subnetCalc.isValid()) {
const networks = subnetCalc.getNetworks();
// Continue as above...
} else {
console.log('Invalid major network or insufficient space for requested subnets.');
}
} catch (error) {
console.error('Error during subnet calculation:', error);
}
`javascript
const VLSMCalc = require('vlsmcalc');
let hostsEachSubnet = [500, 250, 100, 50]; // Hosts required for each subnet
let majorNetwork = '192.168.0.0/24'; // Major network in CIDR notation
// Create subnet calculation
let subnet = new VLSMCalc(hostsEachSubnet, majorNetwork);
// Get networks (subnets)
let networks = subnet.getNetworks();
// Output network details
networks.forEach(network => {
console.log(Network: ${network.getNetwork()});Subnet Mask: ${network.getSubnetMask()}
console.log();First IP: ${network.getFirstIP()}
console.log();Last IP: ${network.getLastIP()}
console.log();Broadcast: ${network.getBroadcast()}
console.log();---------------------------------
console.log();`
});
Network: 192.168.0.0
Subnet Mask: 255.255.255.0
First IP: 192.168.0.1
Last IP: 192.168.0.254
Broadcast: 192.168.0.255
---------------------------------
Network: 192.168.1.0
Subnet Mask: 255.255.255.128
First IP: 192.168.1.1
Last IP: 192.168.1.126
Broadcast: 192.168.1.127
---------------------------------
Network: 192.168.1.128
Subnet Mask: 255.255.255.192
First IP: 192.168.1.129
Last IP: 192.168.1.190
Broadcast: 192.168.1.191
---------------------------------
Network: 192.168.1.192
Subnet Mask: 255.255.255.224
First IP: 192.168.1.193
Last IP: 192.168.1.222
Broadcast: 192.168.1.223
---------------------------------
`$3
`javascript
const { Network } = require('vlsmcalc');let majorNetwork = '192.168.0.0/24';
let network = new Network(500, majorNetwork);
// Get the next available network after allocation
console.log('Next Network:', network.getNextNetwork());
`
`
Next Network: 192.168.1.0
`Contributing
We welcome contributions to this project! If you find any issues or have improvements to suggest, feel free to open an issue or submit a pull request.
Before you contribute, please ensure you check the existing issues to avoid duplicates.
To contribute, follow these steps:
1. Fork the repository.
2. Clone your fork to your local machine.
3. Create a new branch for your changes.
4. Make your changes and add tests if applicable.
5. Run the tests to ensure everything works:
`bash
yarn test
``Your contributions help improve this project, and we appreciate your effort!
Find the source code and report issues at: GitHub Repository.
This project is licensed under the MIT License. See the LICENSE file for details.
Created by Marco De Araujo. For more details, visit marcodearaujo.com or connect with me on LinkedIn.