How to find slash format of an IP address?

0

For example, in this IP address: 145.32.59.24

the slash format should be /16 in the answer.

But I have no clue how this to determine it.

I understand this is a class B IP address.

Please help me.. I have been trying so hard to figure this out.

user2750830

Posted 2014-04-11T13:47:15.897

Reputation: 11

1http://www.vlsm-calc.net/ipclasses.php lists all the class and their subnet masks. Once you know the class, it should be easy to come with 'slash format'. – Vinayak Garg – 2014-04-11T13:59:08.813

1@VinayakGarg, put this as answer. Your solution allows one to successfully determine the 'slash format' - so it answers the question. – VL-80 – 2014-04-11T14:01:13.950

9Practical note: in real life this would work if the Internet was still classfull. However with the introduction of CIDR, IP classes are not being used on the Internet (and most other networks) anymore, so it's impossible to determine the mask of a subnet. – mtak – 2014-04-11T14:03:34.813

Answers

4

Since Classless Internet Domain Routing (CIDR) was introduced in 1993, the ABC "classes" of network/host have become less and less useful and followed. There is no reliable way to determine the partitioning of an IPv4 address into its network and host components from the address alone. The /n format for specifying the netmask assumes a contiguous netmask of that many bits, with the rightmost remainder being the host portion.

So the old class A would be expressed as /8, B as /16, and C as /24. With CIDR, though, you are not limited to those partitions but can split the 32-bit address arbitrarily. You need the netmask to determine the proper /n. Conversely, given the /n, one can determine the netmask. For instance, /16 would have a 255.255.0.0 netmask.

mpez0

Posted 2014-04-11T13:47:15.897

Reputation: 2 578

1

This is called the 'netmask'; not the 'slash format'. If anything, you can call it 'CIDR notation'. The format is generally irrelevant. This will return the netmask in dot-notation format:

#!/bin/sh
ifconfig "$1" | sed -rn '2s/ .*:(.*)$/\1/p'

Just call it like: script.sh eth0 . If you want to you can parse this to create the CIDR notation or you can try to parse it from the output of ip addr which displays in CIDR notation already. I don't have this code handy though.

You may have noticed that neither of these solutions actually take an IP address and spit out a netmask. To do that is not possible. The reason we need to specify a netmask when configuring our IP is because it cannot automatically be determined by the IP. The netmask is a filter to be applied to an IP address. It contains information not inherent to an IP address. Both of these solutions are actually just returning the netmask used to configure the interface.

krowe

Posted 2014-04-11T13:47:15.897

Reputation: 5 031