How to find IP address range

ip addressnmapsubnet

When I use this command which IP addresed are scanned

# nmap -sP 192.168.0.120/25

CAn you please help me how to get the IP range when I have the addres and subnet. Because I am trying to understand this, but no result till now..Thanks in advance

Best Answer

The network in your command is in CIDR notation. The first part (before the /) defines which network, and the second part defines how many bits of netmask are set. An IPv4 address is 4 bytes, or 32 bits of information. /25 means that 25 bits of this address are used to denote the network, and 32 - 25 = 7 bits are left to address hosts on the network. A /25 network can hold 2^7 = 128 hosts, less the network and broadcast addresses. To get the network address (the start of your block of addresses), you take the address given and bitwise-and it with 2^32 - 2^7. In this case (using Python):

>>> # Get the integer value of the address
>>> import struct
>>> ip = struct.unpack(">I", struct.pack("4B", 192, 168, 0, 120))[0]
>>> bin(ip)
'0b11000000101010000000000001111000'
>>> # Bitwise-and with the netmask
>>> net = ip & (2**32 - 2**7)
>>> bin(net)
'0b11000000101010000000000000000000'
>>> # Convert back to dotted-decimal
>>> struct.unpack("4B", struct.pack(">I", net))
(192, 168, 0, 0)

So the network address is 192.168.0.0, and you have 128 addresses, so your target range is 192.168.0.0 - 192.168.0.127.