How to find IP address range

倖福魔咒の 提交于 2019-12-11 00:38:12

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/19907637/how-to-find-ip-address-range

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!