Easily create subnets using this ingenious Python script

0liverFlow
5 min readMay 19, 2023

--

SubnetCalculator Banner

Subnetting is a technique that consists of breaking a large network into smaller networks or sub-networks.

Dividing a network into smaller networks has several advantages namely:

  • Manageability: you can easily manage devices on each sub-network.
  • Performance: network subnetting can help reduce congestion. This contributes to reduce collisions and by the way having a better latency and jitter (variation of the latency).
  • Security: network subnetting helps to separate hostile devices from sensitive devices assuring a better security.

Through this article, you will learn how to divide a network into sub-networks by using a versatile and interesting Python tool called SubnetCalculator that i developed for that purpose.

Specifically, you will learn how to use SubnetCalculator to:

  • Determine the network address, the broadcast address as well as the range of IP addresses from a given network.
  • Divide a given network into equal-sized subnets (FLSM).
  • Divide a given network into subnets of different sizes based on the number of hosts required (VLSM).

That being said, let’s get started guys :)

Usage

SubnetCalculator is a python script which allows you to easily divide a network into sub-networks.

It is intuitive to use, and you can find all the information you need by simply taking a look at the help menu as follows:

SubnetCalculator usage

NOTE📝: Don’t hesitate to download the GitHub repository and test the features with me. All you need is a python version greater or equal to 3.10.

Excellent, after having a better understanding of how SubnetCalculator works, let’s take a look at how to get the network and broadcast address from a given network.

Network Address, Broadcast Address, and IP Range

In this section, we’re going to use SubnetCalculator to determine the network and broadcast address as well as the IP range from a given network.

To do that, you only need to specify the --network option followed by a network in CIDR or slash notation.

Here is an illustration:

Getting Network Address, Broadcast Address and IP Range using SubnetCalculator

As you can see, the script returned us the Network Address, the Broadcast Address and the IP Range (First and Last address) based on our given network (192.168.1.0/24).

Moreover, this is represented in a very beautiful table for more readability.

Feel free, to use networks of your choice 😉.

NOTE📝: It is important to keep in mind that the --network option is mandatory and its value must be a network in CIDR notation.

Super, let’s now be focused on how to divide our networks into a number of equal-sized subnets.

Fixed-Length Subnet Mask (FLSM)

Fixed-Length Subnet Mask is a type of subnetting that consists of dividing a given network into equal-sized subnets.

In this type of subnetting, all subnets within a network use the same subnet mask and therefore have the same number of hosts.

SubnetCalculator can help you divide your network using the FLSM technique.

To do that you only need to specify the --flsm option followed by the number of subnets you want.

Here is an illustration:

Dividing a given network using FLSM technique

As you can see, SubnetCalculator divided our given network (192.168.1.0/24) into three equal sub-networks.

That sounds great! Right?

However, what if we’d like instead three subnets with different number of hosts?

Let’s say, we’d like one subnet with 12 hosts, another one with 24 hosts and the last one with 96 hosts.

In that specific case, the FLSM technique can be inefficient and that’s where VLSM comes into play.

No worries if VLSM doesn’t mean much to you, that’s what we’re going to see in the next section.

Variable Length Subnet Mask (VLSM)

Variable Length Subnet Mask is a type of subnetting that consists of dividing a given network into subnets of different sizes based on the number of hosts required.

Contrarily to FLSM where all subnets have the same number of hosts, each subnet in VLSM can have a different number of hosts.

That provides flexibility in network design and allows for efficient allocation of IP addresses.

To divide your network using SubnetCalculator and the VLSM technique, you only need to specify the --vlsm option followed by the number of hosts in each network as follows:

Dividing a given network using VLSM technique

As you can see, SubnetCalculator divided our network (192.168.1.0/24) into three subnets of different sizes.

NOTE📝: It is important to separate the number of hosts by a space, otherwise the script will raise an exception.

Great! Let’s now sum up what we have seen so far.

Let’s recap

Subnetting is a very important topic in networking and having a good understanding of how it works can be useful to you in your day to day life as a network or cybersecurity engineer.

In this article, we have seen how to utilize SubnetCalculator to automate this process and divide a given network using various techniques such as FLSM and VLSM.

Last but not least, do not hesitate to leave your feedback on the GitHub repository. This script takes me lots of time to develop and a little star ⭐️ will be highly appreciated 😉.

That’s all dear readers! Hope you learnt something!

Do not forget to click on the little clap icon below if you enjoyed the content.

Furthermore, thanks for subscribing to my newsletter to keep up with my latest articles.

--

--

0liverFlow
0liverFlow

Written by 0liverFlow

Pentester | Enjoy breaking and building stuffs

No responses yet