How to Calculate the Ip Range When the Ip Address and the Netmask Is Given

How to calculate the IP range when the IP address and the netmask is given?

my good friend Alessandro have a nice post regarding bit operators in C#, you should read about it so you know what to do.

It's pretty easy. If you break down the IP given to you to binary, the network address is the ip address where all of the host bits (the 0's in the subnet mask) are 0,and the last address, the broadcast address, is where all the host bits are 1.

For example:

ip 192.168.33.72 mask 255.255.255.192

11111111.11111111.11111111.11000000 (subnet mask)
11000000.10101000.00100001.01001000 (ip address)

The bolded parts is the HOST bits (the rest are network bits). If you turn all the host bits to 0 on the IP, you get the first possible IP:

11000000.10101000.00100001.01000000 (192.168.33.64)

If you turn all the host bits to 1's, then you get the last possible IP (aka the broadcast address):

11000000.10101000.00100001.01111111 (192.168.33.127)

So for my example:

the network is "192.168.33.64/26":
Network address: 192.168.33.64
First usable: 192.168.33.65 (you can use the network address, but generally this is considered bad practice)
Last useable: 192.168.33.126
Broadcast address: 192.168.33.127

Given IP address and Netmask, how can I calculate the subnet range using bash?

Well, you already have the network address. The first host address is just one higher than the network address, which is easy to calculate since you know the low-order bits are zeroes (so there's no overflow to high bytes...)

Then the broadcast address. That's just the address where all the host address bits are set to ones. Those are the bits where the subnet mask is zero. So, to get the broadcast address, invert the mask and do a bitwise or. The last host address is just one less from that.

Bash's arithmetic supports the same bitwise operators as C and most other languages, so & for and, | for or, ^ for xor and ~ for negation. From what you already have, you should be able to produce the missing ones.

(And yes, doing that with the shell seems a bit icky, but if you're going to implement the calculation manually it's going to be pretty much the same in any programming language.)

How can i calculate ip address range from subnet mask

192.168.0.120

This says that the IP address is 192.168.0.120.

/25

This says that the netmask is 25 bits long. As an IPv4 address is 32 bits, that leaves 7 bits for the address. The lowest IP address in the range is given by masking out the bottom 7 bits, and the highest by adding 127 (=27-1) to that.

Calculating range of IPs from subnet mask

A netmask is a series of 1 bits. The bits must be sequential with no 0 gaps. Anything using a 1 bit is part of the network, anything remaining is valid for host assignment within that network. A 255.255.255.224 has 27 "1" bits, which means it's a /27 network.

To calculate this right, you need to convert IPs to a numeric representation. For example, 255.255.255.224 is 11111111 11111111 11111111 11100000 which is 4294967264. 192.168.1.101 is 3232235877 (11000000 10101000 00000001 01100101).

If you take the IP and bitwise AND it with the netmask, that gives you the network address. This is the bottom end of the range:

11111111 11111111 11111111 11100000  (mask)
11000000 10101000 00000001 01100101 (ip)
-----------------------------------
11000000 10101000 00000001 01100000 = 192.168.1.96 (network address)

The complement (bitwise NOT) of the mask gives you the size of the range:

00000000 00000000 00000000 00011111  = 31

Thus, the range for that IP is between 192.168.1.96 - 192.168.1.127. (127 = 96 + 31)

Calculate IP range by subnet mask

To determine the adress range follow these steps :

1) Take your subnet mask (here 255.255.255.0) and convert it in binary :

11111111.11111111.11111111.00000000

( 8 + 8 + 8 + 0 = 24 -> So you can write your ip adresse like this : 192.168.1.x/24 because you are in a /24 network)

2) You have in a /24 network 256-2=254 usable ip adresses for host (one is for the network adress (the first in your range) and the other one is for the broadcast adress (the last in your range)).

3) To get your range simply get your network adress (the first ip adress according to your subnet mask) and get the next 255 ip addresses and you'll have your range.

Your network adress:

In binary the last octet has to be null :

xxxxxxxx.xxxxxxxx.xxxxxxxx.00000000

Your broadcast adress:

In binary the last octet has to be equal to 1:

xxxxxxxx.xxxxxxxx.xxxxxxxx.11111111

Here your ip adress is 192.168.1.5.
In binary we get:

11000000.10101000.00000000.00000101
  1. Your network address: 11000000.10101000.00000000.00000000 <->
    192.168.1.0
  2. Your broadcast address: 11000000.10101000.000000000.11111111 <->
    192.168.1.255
  3. First usable ip address: 192.168.1.1

  4. Last usable ip address : 192.168.1.254

Hope you enjoyed reading bad english.
Tell me if you have any question,
Loris

Calculate Subnetmask from IP range

Your answer is correct, except that classful addresses don't exist anymore. The internet moved to Classless Inter-Domain Routing (CIDR) in 1993 so your terminology is a bit outdated ;)

IP networking these days works with routing prefixes. A prefix is a range of IP addresses defined by the first address in that range and the number of fixed bits at the start of the address. Your example shows this nicely.

Your example range is 99.224.0.1 - 99.239.255.254. Actually it is 99.224.0.0 - 99.239.255.255 because when used on a subnet the first and last addresses are reserved (but still part of the subnet and prefix).

The first address in the prefix we already have: 99.224.0.0. You can see the prefix length from your binary calculation (slightly modified):

01100011.11101111.11111111.11111111
01100011.11100000.00000000.00000000
----------------------------------- (xor)
00000000.00001111.11111111.11111111
----------------------------------- (not)
11111111.11110000.00000000.00000000

Just count the number of 1s at the beginning: 12. So your prefix is 99.224.0.0/12. This prefix covers all addresses that match 01100011.1110****.********.********.

When writing the prefix length down as a subnet mask you indeed get 255.240.0.0.


A little off-topic here because it is about networking and not about the algorithm to calculate the subnet mask, but maybe helpful: an example of how you can plan network addressing:

Lets say that for my office building I get IP addresses 192.0.2.0/24 (that is 192.0.2.0 - 192.0.2.255, subnet mask 255.255.255.0, 256 addresses). I need 50 addresses for servers, 100 addresses for employee devices and 40 addresses for guests.

Because addressing works with prefixes everything you get is a power of 2. If you use a /24 you have a prefix with 256 addresses. The full address is 32 bits, the first 24 are fixed so you have 8 bits left to use. 28 = 256. If you use a /25 you have a prefix 128 addresses, a /26 has 64 addresses etc.

That way you can also split up a prefix. 192.0.2.0/24 can be split up into 192.0.2.0/25 and 192.0.2.128/25. And those can be split again and again until you have a prefix that covers only a single address: a /32.

back to the example. To get (at least) 50 addresses for the servers I need to round up to the next power of 2. That is 26 = 64. To have that many addresses I need a /26 prefix. For the client devices I need to round up to 128 (27) so we need a /25. For the guests the next power of 2 is 64 (26) so a /26.

So we need to split up out /24 into a /25 and two /26s. One possible solution is:

Client devices:  192.0.2.0/25
Servers: 192.0.2.128/26
Guests: 192.0.2.192/26

Once we configure these subnets on our devices the first and last address of each subnet become special (the network and broadcast address) so we can use these ranges for our devices:

Client devices:  192.0.2.1   - 192.0.2.126
Servers: 192.0.2.129 - 192.0.2.190
Guests: 192.0.2.193 - 192.0.2.254

Determine Network IP when given an IP and subnet mask

The module ipcalc makes quick work of working with ip addresses as string:

Code:

import ipcalc

addr = ipcalc.IP('10.0.0.20', mask='255.255.255.0')
network_with_cidr = str(addr.guess_network())
bare_network = network_with_cidr.split('/')[0]

print(addr, network_with_cidr, bare_network)

Results:

IP('10.0.0.20/24') '10.0.0.0/24' '10.0.0.0'


Related Topics



Leave a reply



Submit