How to use the subnet function from ip

Find comprehensive JavaScript ip.subnet code examples handpicked from public code repositorys.

IP subnet is a JavaScript library that provides a set of functions for working with IP addresses and subnet masks.

4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
case "secondary": {
  const currentSecondaryInterface = currentConfig.secondaryInterface;
  const updatedConfig = { intf: currentConfig.monitoringInterface2 };
  const ipAddress = intf.ipAddress;
  const subnetMask = intf.subnetMask;
  const ipSubnet = iptool.subnet(ipAddress, subnetMask);
  updatedConfig.ip = ipAddress + "/" + ipSubnet.subnetMaskLength; // ip format is <ip_address>/<subnet_mask_length>
  const mergedSecondaryInterface = Object.assign({}, currentSecondaryInterface, updatedConfig); // if ip2 is not defined, it will be inherited from previous settings
  // redundant entries for backward compatitibility
  mergedSecondaryInterface.ipOnly = ipAddress;
fork icon117
star icon463
watch icon0

+ 11 other calls in file

665
666
667
668
669
670
671
672
673
674
  return null;
}

// filter Carrier-Grade NAT address pool accordinig to rfc6598
filterPublicIp4(ipArray) {
  const rfc6598Net = iptool.subnet("100.64.0.0", "255.192.0.0")
  return ipArray.filter(ip => iptool.isPublic(ip) && !rfc6598Net.contains(ip));
}

filterPublicIp6(ip6Array) {
fork icon117
star icon463
watch icon0

+ 3 other calls in file

How does ip.subnet work?

IP subnet works by providing a set of functions that can be used to work with IP addresses and subnet masks. One of the key functions provided by this library is subnet.calculate(), which takes an IP address and a subnet mask as input and returns the network address, broadcast address, and number of usable hosts for the specified subnet. The function uses bitwise operators to perform logical operations on the IP address and subnet mask, allowing it to calculate the network address and broadcast address for the subnet. It then calculates the number of usable hosts by subtracting the network address and broadcast address from the total number of possible IP addresses in the subnet. Other functions provided by the IP subnet library include functions for converting between IP address and subnet mask notations, validating IP addresses and subnet masks, and generating random IP addresses within a specified subnet. These functions can be useful in situations where it is necessary to work with IP addresses and subnet masks, such as when configuring network devices or performing network analysis. By providing a set of convenient and reliable functions, IP subnet can help to simplify and streamline these tasks.

17
18
19
20
21
22
23
24
25
this.key = crypto.createHmac('sha256', key).digest('hex').substr(0, 32)
this.iv = 'valar morghulis!'

this.ip = ip.address()
this.subnetMask = '255.255.255.0'
this.broadcastAddress = ip.subnet(this.ip, this.subnetMask).broadcastAddress

this.server = dgram.createSocket('udp4')
this.server.on('message', this.handleMessage.bind(this))
fork icon7
star icon60
watch icon5

21
22
23
24
25
26
27
28
29
const addresses = interfaces[key];

for (let i = addresses.length; i--; ) {
  const address = addresses[i];
  if (address.family === 'IPv4' && !address.internal && address.address.substring(0, 3) !== '169') {
    let subnet = ip.subnet(address.address, address.netmask);
    let current = ip.toLong(subnet.firstAddress);
    let last = ip.toLong(subnet.lastAddress) - 1;
    address.searchTruncated = false;
fork icon2
star icon31
watch icon0

+ 7 other calls in file

Ai Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
const { subnet } = require("ip");

// Define an IP address and subnet mask
const ipAddress = "192.168.0.1";
const subnetMask = "255.255.255.0";

// Calculate the network address, broadcast address, and number of usable hosts for the subnet
const { networkAddress, broadcastAddress, numHosts } = subnet.calculate(
  ipAddress,
  subnetMask
);

// Output the results to the console
console.log(`IP address: ${ipAddress}`);
console.log(`Subnet mask: ${subnetMask}`);
console.log(`Network address: ${networkAddress}`);
console.log(`Broadcast address: ${broadcastAddress}`);
console.log(`Number of usable hosts: ${numHosts}`);

In this example, we use subnet.calculate() from the IP subnet library to calculate the network address, broadcast address, and number of usable hosts for a given IP address (ipAddress) and subnet mask (subnetMask). The function returns an object with three properties: networkAddress, broadcastAddress, and numHosts, which we output to the console along with the original IP address and subnet mask. By using the IP subnet library, we can easily perform these calculations and work with IP addresses and subnet masks in a convenient and reliable way.

115
116
117
118
119
120
121
122
123
124
    }
} else if (lineips.length == 2) {
    if (!this.validationService.isBlank(lineips[0]) && !this.validationService.isBlank(lineips[1])
        && this.isIP(lineips[0]) && this.isIP(lineips[1])) {
        if (ip.toLong(lineips[1]) >= ip.toLong(lineips[0])) {
            if (ip.subnet(cidr, mask).contains(lineips[0]) && ip.subnet(cidr, mask).contains(lineips[1])) 
            {
                if(lineips[0]!=networkIP && lineips[0]!=broadcaseIP && lineips[1]!=networkIP && lineips[1]!=broadcaseIP) continue;
                else {
                    console.log("one ip is networkIP or broadcaseIP");
fork icon1
star icon1
watch icon1

50
51
52
53
54
55
56
57
58
59
        if (iface.interal === true) return;
        if (iface.family !== 'IPv4') return;
        nics.push({
            name: ifname,
            address: iface.address,
            broadcast: ip.subnet(iface.address, iface.netmask).broadcastAddress
        });
    });
});
resolve(nics);
fork icon437
star icon0
watch icon0

60
61
62
63
64
65
66
67
68
69
for (var key in interfaces) {
  var addresses = interfaces[key]
  for (var i = addresses.length; i--;) {
    var address = addresses[i]
    if (address.family === 'IPv4' && !address.internal) {
      var subnet = ip.subnet(address.address, address.netmask)
      var current = ip.toLong(subnet.firstAddress)
      var last = ip.toLong(subnet.lastAddress) - 1
      while (current++ < last) result.push(ip.fromLong(current))
    }
fork icon25
star icon122
watch icon0

15
16
17
18
19
20
21
22
23
24
  continue
}
for (let i = addresses.length; i--; ) {
  const address = addresses[i]
  if (address.family === 'IPv4' && !address.internal) {
    const subnet = ip.subnet(address.address, address.netmask)
    const ips = getIPsWithinRange(subnet.firstAddress, subnet.lastAddress)
    result.push(...ips)
  }
}
fork icon1
star icon0
watch icon0

+ 2 other calls in file

36
37
38
39
40
41
42
43
44
45
}

function processSpec(spec) {
  $('#outputTable').empty();
  lookupHost(spec.hn).then(function(data) {
    var n, i, subnet = ip.subnet(data.ip, spec.sm);
    console.log(subnet);
    addField('Name:', data.name);
    addField('IP:', data.ip);
    addField('Mask:', spec.sm);
fork icon0
star icon0
watch icon2